Invoke and BeginInvoke

前端 未结 5 1751
盖世英雄少女心
盖世英雄少女心 2020-12-29 06:30

Greetings, I am developing some application in C#. At the moment I\'m dealing with threading and I have a question that I have in my mind. What is the difference between In

5条回答
  •  长发绾君心
    2020-12-29 07:07

    To begin, from your link:

    • Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
    • Control.BeginInvoke: Executes on the asynchronous UI thread, and calling thread doesn't wait for completion.

    and from MSDN:

    BeginInvoke executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.

    To sum it up, BeginInvoke is asynchronous. When BeginInvoke is called from the UI thread the request will be executed in parallel with the UI thread. Which means it may not execute until after the currently executing method has returned. So in this case the text box will never appear to update because the for loop will not be interrupted, as the calling thread will not wait for this event to be completed before continuing.

    Alternatively, Invoke is synchronous. The text box will be updated because the calling thread will wait for the call to complete before continuing execution.

提交回复
热议问题