Extra information about Invoke
I would expect you would have added the comment you placed as answer about invoke. But here's the answer anyway :-)
The reason this is posted as another answer is because I got an error when trying to add it as a comment (I think because of the size of it)
When a thread wants to update the user interface, this cannot be done directly. The main reason for that, is that the UI is event driven. Meaning that there must be some action to get any reaction. The main program loop that is responsible for the UI (repainting, handling button clicks, etc...) waits for a message by repeated calls to the OS. This is a so called message loop (
http://msdn.microsoft.com/en-us/library/ms644936%28VS.85%29.aspx[
^]) that helps receiving input in an efficient and generic way. This also a reason why you should use threads for intensive processing because the OS will report your application as "not responding" if you do not occasionally handle these messages.
Well, this is some information aside but actual one of the main reasons why invoke is used for handling UI updates. When you would update the UI from another thread than the main thread, the actual UI would not be updated because there was no request to do this and no actual repaint event was fired. It also would be possible that multiple threads besides the main thread are doing screen updates without any synchronization what so ever. This is not excepted and will throw an exception. To overcome this, the actual change should be made in the "context" of the main thread (You could say that "context" is the workplace of a certain thread). This means that a mechanism must be used, in such a way that the actual code you would like to be executed, get transfered to the main thread and let that thread execute it. Because the main thread is caught up in handling incoming messages, the most easy way is to simply pass it a message which makes it clear that we would like it to execute certain code. To minimize the size of the message and overhead we don't send it the code we would like to execute, but use an powerful trick that was just discussed earlier... as you might already guess "delegates". A thread, other than the UI thread, will send a message with the request to execute a certain function, that was passed as delegate (function address) with some parameters that must be passed to that function. This messages is picked up by the main thread and then executed, which handles only one message at a time, so even if there are a lot of threads that are all sending an occasional message to the main thread, these are each handled one by one, providing a very nice method of synchronization.
Some extra info: In .net you will often encounter a check to see if invoke is actually necessary. This is done by a check using InvokeRequired. This is of course to prevent it from invoking over and over again. Most threads that update the UI have a specific function for this that will check if invoke is required and invoke themselves if this is needed. The function will then be called by the main thread and the outcome of the check to see if invoke is required will be false. The necessary screen updating can then be done safely. This way the function is providing screen updating and invocation when necessary all by itself. Of course the check to see if invoke is required is simply to compare the current thread (obtained by GetCurrentThread) and the main window thread (obtained by GetWindowThreadProcessID) and if these are not equal an invoke is required.
There is also another type of invocation called Pinvoke. For more information on that I would recommend checking out the following link:
Using Platform Invoke[
^]
Hopefully this gave you a good idea about invoke.