Windows Forms UI Threading
I fixed a UI issue we were having where some work was being done and it was updating a progress bar, but it wasn’t correctly painting the rest of the labels and other static UI elements. It’s best to let the UI do it’s thing on the main thread, and then spawn off a worker thread in the background, allowing them to communicate with each other through delegates.
Here is a very nicely done, informative article about delegates that I should have read (I probably would have gotten to my solution quicker and surpassed my trial and error issues): http://www.ondotnet.com/pub/a/dotnet/2002/11/04/delegates.htm
I used the following two articles as a basis for my fix:
- http://msdn2.microsoft.com/en-us/library/ms951089.aspx
- http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelegates.html?page=1
However, I didn’t end up using the MulticastDelegate class at all in my solution. It’s not really necessary, and I couldn’t get it to work when I tried to extend or inherit from it. The following article says you can’t anyway, unless you are a compiler or some similar tool: http://msdn2.microsoft.com/en-us/library/system.multicastdelegate.aspx
This link has a cool helper class for asynchronous delegates, though I didn’t use it: http://peach.ease.lsoft.com/scripts/wa.exe?A2=ind0302B&L=ADVANCED-DOTNET&T=0&F=&S=&P=31992
Good luck.
FOLLOWUP (05/21/09): I have some further clarifications about how to properly invoke delegates to a new background thread in this post: