Understanding Invoke method

Hello all,
I started recently working with .NET/C#/WindowsForms, coming from Win32/MFC programming. So, all my apologies if you feel I am too much trying to find similarities between both environments.

I have a UI application controlling the execution of a background worker thread. The thread is launched when the user presses a button, and must report about the current task being done, to the UI main thread (removing file #### , calculating something, etc.) -- no revolution in computing area ...:-))

For information, I am not using a BackgroundWorker instance, but a Thread object.

I saw in the documentation, that, to make the communication thread-safe (I mean : updating my UI with the last status generated by the worker thread), I have to use the pair of InvokeRequired/Invoke methods to guarantee that the update of the UI will be executed in the main thread.

OK. But ... What does the Invoke method
Under Win32/MFC, I used to post messages from the worker thread to the main thread message queue, assuming then that the main thread message pump would then unstack them and handle them as normal messages, and no risk of concurent UI updates.

Can I consider the Invoke method call to act as a message queuing method, or should I take care before updating the UI that normally monitored and updated through the message pump I mean : is there any risk to get conflicts between the user actions and the actions ordered by the worker thread

Hoping my question is clear ...
Thanks again.


Answer this question

Understanding Invoke method

  • mikets

    Invoke and BeginInvoke work like SendMessage and PostMessage in Win32. They are handled in the UI thread, this is exactly the purpose of Invoke and BeginInvoke. Calls done by these functions are serialized and handled in the same queue as message handlers like button press, Paint etc. I don't know whether they are implemented using Windows messages, but you can use them by the same way. There are no conflicts with user actions.
  • Serge Bollaerts

    Thank you for this quick answer.


  • Understanding Invoke method