All,
I was just reading over a new MSDN Magazine article by Juval Lowy on the
new features in C# 2.0 (its a wonderful article, see link below.) In
the section titled "Anonymous Method Example" he makes reference to an
aspect of .NET 2.0 that may have a significant impact on many of your
applications, if they are multi-threaded and have a user interface.
Now we all know that the right way to update any parts of the user
interface in a multi-threaded application is to marshal the call to the
thread that created the interface control and perform the updates there.
But (be honest now) how many of us really do that for 100% of our calls?
I know I don't (maybe I'm the only one??). Apparently .NET 2.0 will
force you to do this - throwing an exception if you don't.
Note that I'm not complaining - this is surely a good thing for program
realibility. I'm just giving a heads up to those of us that tend to
relax the rules on occasion... :)
However this does bring up a question - how do most people deal with
this? Specifically...
When I do things the right way, things seem a bit cumbersome. I need a
delegate and a separate method for any function which updates the
interface.
So now I have to "control.Invoke (delegate, new object[] { ... });" which
seems to (a) make the code difficult to read (b) creates object[]'s all
over the place.
Now I've only been programming .NET for about a year, but I haven't seen
anything to indicate if there is there a better way to do it? And more
to the point, why didn't the .NET framework programmers build in thread
safety into the .NET WinForms components??
Link to article by Juval Lowy:
http://msdn.microsoft.com/msdnmag/is...0/default.aspx
-mdb