Tips and Tricks

Performance Tips for VS2010

I found out today that, if you go to Help >> Troubleshooting in VS2010 (SP1), you’ll get lots of cool performance recommendations. You can also see these at: http://msdn.microsoft.com/e...

RemoveHandler Issues with Custom Events

This is a case of things being more complicated that I thought they should be. Since it took a while to figure this one out, I thought it was worth explaining and putting all of the pieces to the answer in one spot. Let me set the stage. Architecturally, I have the notion of generic producers and consumers. These put items onto, and remove items from, a queue. This provides a generic, thread-safe mechanism to load balance the creation and processing of work items in our application. Part of the IProducer(Of ...

Regular Expressions in Visual Studio “Find”

I had a case today where I wanted to find all the spots within our application that a developer had explicitly set the BackColor property of a control at run-time. As you can imagine, a simple search ended up with my finding a ton of designer code where the generated code was setting the property. (It sure would be nice if Visual Studio would let you exclude comments and designer files from the search – but that’s not in VS2008 or VS2010 Beta 1, unless it’s well hidden in both.) Wildcards seems like ...

Oracle + Azure = Crash and Burn

One of the downsides to Azure is that it’s error messages need a LOT of work. I just installed the July CTP of Azure. After doing so, even the simplest cloud project would fail. I’m talking “Create New Project” then F5… wait 30 seconds… bomb. I kept getting the ubiquitous “Role instances did not start within the time allowed. Please try again. If you continue to encounter this behavior please try shutting down the Development Fabric." message. Nice and descriptive. Digging into the problem, it looked ...

Numeric Chicanery

For those of you who have been around since the VB6 days, you surely remember the interesting rounding behavior of CInt(x). Specifically, it rounded a number ending in .5 to the nearest even number. That is, CInt(2.5) rounded to 2 and CInt(3.5) rounded to 4. This behavior still exists in VB.NET. No real surprise there. And, to be honest, I don’t mind it that much. When I see “CInt,” I really don’t have any preconceived, intuitive understanding of what it does. Math.Round(x) is another question entirely. ...

Deploying WCF Services to Azure

My past experience with WCF services has followed a slightly unusual path. For a variety of reasons, most of the services I've written have consisted of an interface and an implementation. Pretty straight-forward thus far. However, where it gets a little unusual is in the hosting mechanism. We're using Windows services to host of WCF services. This works out well for us, since these services live on intranets, and aren't publicly exposed. When looking at Azure, services are a little different. It's ...

The Developer's Way to Convert Code

All right, all you developers out there... let's see a show of hands. How many of you delight in finding new ways to solve a problem? You. Yes, you in the back. Get your hand up. You can't call yourself a developer if you don't enjoy finding a new (preferably somewhat convoluted) to solve a problem. I've been doing some work that involves converting C# code to VB.NET code. I was sitting in the speaker lounge at VS Live, shortly after getting into San Francisco. I'd played a little bit on the plane ...

VB Dev Center Article Published

The work I did on benchmarking DataTable performance (or lack thereof *cough cough*) has been published in an article on the MSDN VB Dev Center. For those of you wondering about the results I mentioned a few months ago, they're included there. I've also included some work on benchmarking LINQ select queries and aggregation in comparison to the DataTable equivalents. Many thanks to Beth Massi for helping get this published ...

Try/Catch Performance

I'm really not obsessed with performance -- honest! However, when a co-worker asked me today exception handling was an acceptable way of coding defensively, my reaction was rather predictable. Exceptions are pure evil, and should be... well, exceptional. Yes, you guessed it. The next question was "How bad is try/catch really?" The short answer is that is involves minimal overhead... unless an exception is thrown. In that case, the .NET exception handling mechanism does a few nice things, like providing ...

Azure Hands-On Labs Issue

If you should happen to create a new cloud services project in VS2008 with VB settings, you may well find that you are unable to save the project/solution. The message is, in typical fashion, singularly useless: Luke H's blog pointed me in the right direction. The solution is to enable saving projects on creation. Under Tools >> Options, select Projects and Solutions. Make sure "Save new projects when created" is checked. Restart Visual Studio, and you'll be good to go ...

Using Exception Handling to Retry an Operation

I got the following question via e-mail: I want to catch timeout errors when I execute a script. Catching the error is easy, but I then want to retry 3 times. In VB6 I could just resume a command when I caught the error. How can I do this with vb 2008 using try..catch? This is a case where two different techniques come into play. The first is using a number of Catch statements to allow you to catch specific types of exceptions. The second is to make essentially a recursive call to the function, with ...

LiveWriter Code Formatting Plug-In

Another thing that took me far too long to find today... there's a lot of links to Steve Dunn's tool, including the CodePlex version, which hasn't been updated since 2006. And, since it doesn't have an installer or the DLLs readily available, you'll waste a lot of time looking for that too. You might consider trying this tool. Looks pretty good to me so far. And, yes, it supports a few languages. And line numbers. And highlighting. Public Class ThisRocks Public Function URL As String Return "http://www.amergerzic.com/... ...

VB.NET Generics with Multiple Constraints

I had a case today where I needed -- well, wanted -- to implement a generic class that was a little... unusual. Essentially, I'm working on a queuing mechanism based on Joe Duffy's BlockingBoundedQueue(Of T). Mind you, unlike Joe's sample, mine is written in VB.NET. ;) Taking it a bit further, I created a wrapper that specifies the number of producers and consumers of queued objects, as well as the queue capacity. Since I'm going to be turning this over to a bunch of developers to play with, I also ...

DataTable Performance Update

In a fairly old post, I promised graphs related to the performance of DataTables vs. other data access mechanisms. I've got an article almost ready for publishing on that topic -- should be out by the end of the week, and I'll link to it when it's public. Until then, let me throw out a bit of a teaser... The benchmarking code, as well as detailed numbers and analysis will be in the article ...

Creating and Calling Extensions Methods

One of the challenges in introducing new language features into our code base is that they're not always well understood. As a result, some interesting discussions come up. Today's subject is extension methods -- what are they, how to write them and how to use them. Let's start by examining a piece of code I wrote while doing some dynamic SQL parsing. There was a lot of code to remove the trailing " AND " from my criteria strings that looked like: s = s.Substring(0, s.Length - 5) I got tired of having ...

Performance -- DataTable.Select vs Dictionary

Kathleen Dollard made an offhand comment a while back that List(Of T) should be a better option than DataTable in many cases. Today, I finally got around to benchmarking some various flavors of DataTable vs. Dictionary. Let me begin by stating the caveat that my benchmarks cover only the scenario where there is a known primary key that is the term used for DataTable.Select. I also only concerned myself with name/value pairs -- at the moment I'm really looking at a "lookup table" sort of scenario. ...

System.Type.GetType performance

Recently, I saw some code that used System.Type.GetType("System... to get a type. My gut feel was that this was bad practice -- even without knowing that this was using reflection, it seemed like an error-prone was to do things. After all, I don't type as well as I'd like, and am apt to mis-type string literals... which the compiler doesn't catch. However, convincing developers to change because "I think that's ugly" can be a tough sell. So I benchmarked it for grins and giggles. Here's the ...

Make sure the speech API is installed

This should go without saying, but I've had recent problems with people not understanding this concept. If you're using System.Speech in Windows XP, you need to make sure that the speech recognition engine from SAPI 5.1 is installed. If you're deploying an application, make sure you include the SAPI 5.1 merge module in your installer

Dynamically Building Custom Grammars

One of my favorite features of System.Speech is the ability to generate custom grammars dynamically. As I've mentioned earler, this took about 50 lines of code in SAPI 5.1. The System.Speech team has included some very nice coarse-grained methods to accomplish this same task in just four lines of code: Protected WithEvents reco As SpeechRecognizer Public Sub LoadGrammar(ByVal options As List(Of String)) Dim choices As New Choices(options.ToArray) Dim gb As New GrammarBuilder(choices) Dim g As New ...

String.Format performance

Since I work with a lot of data-centric applications, I've come to really like String.Format. There's something nice about seeing all your single quotes without them being obscured by double quotes. For example, if I were adding a call to javascript function as an attribute of a control for use in client-side validation of an aspx page, I can use chk.Attributes.Add("onclick", String.Format("updateSelect... workTicketId, chk.ClientID)) instead of chk.Attributes.Add("onclick", ...

Full Tips and Tricks Archive