DCSIMG
April 2009 - Posts - Use the source, Luke...

April 2009 - Posts

The story of .Net 3.5 sp1 and the Evil Dispatcher.BeginInvoke()

Recently Microsoft released a service pack (sp1) for .net 3.5 and another for .net 3.0 (sp2).

The above update caused a very nasty problem which I’ve initially attributed to a lapse in attention on my part, but later, when sifting through the web I learned that many people ran into the same issue.

Basically after upgrading your .net platform from 3.5 to 3.5sp1 or from 3.0 sp1 to 3.0 sp2, Among other things, Dispatcher.BeginInvoke() received another two new overloads:

Until now we had (Supported in: 3.5, 3.0):

Dispatcher.BeginInvoke(DispatcherPriority, Delegate);

Dispatcher.BeginInvoke(DispatcherPriority, Delegate, Object);

Dispatcher.BeginInvoke(DispatcherPriority, Delegate, Object, Object[]);

New overloads (Supported in: 3.5 SP1, 3.0 SP2):

Dispatcher.BeginInvoke(Delegate, Object[]);

Dispatcher.BeginInvoke(Delegate, DisptacherPriority, Object[]);

The last two new overloads, will cause an out of date .net platform to throw a MissingMethodException! (and will not compile).

If that was all there is to it, I wouldn’t have bothered with this post, however, that wasn’t the whole deal. It turns out, that intellisense in Visual Studio stopped suggesting the older methods and only the new ones. Since the new overloads signature bears a striking resemblance to the older ones, it is easy to see how someone might overlook this small change…

Now why do I call this evil ?

Assuming you are working on a Visual Studio project with .Net 3.5 / 3.0 compatibility, by using these methods you just broke it, at least until everyone will upgrade their 3.5 / 3.0 to the respective service packs. There are no compiler warnings / errors and you  might even miss this entirely until you get a MissingMethodException on another computer during runtime! This, in my small world, qualifies as evil :)

The solution of course is to use the older methods, even though they no longer appear in the intellisense suggestions list.

I was looking for a clue as to why these new overloads were introduces, but couldn’t find anything (if someone finds something please say so). Some of the old overloads like Disptacher.BeginInvoke(DispatcherPriority, Deleget, Object, Object[]), seem abit weird, so maybe that is the reason why the new ones were introduces…

Now I have to wonder, what was passing on in the mind of the person who introduced these overloads? is this one of those miraculous bugs or is it just a quiet way to force the usage of the new overloads ?

In any case, its horrible and it cost me quite some time to figure it out. So I hope this post would save you some.