.NET Invoke Process Flow

I see / use some form or code mode all the time:

public void method1(Object sender, EventArgs args)
{
  if(dataGridView1.InvokeRequired)
    dataGridView1.Invoke(new EventHandler(method1), null);
  else
    // Do something to dataGridView1
}

My question is: what happens to the GUI thread when I use Invoke? This is like an interrupt, where will the thread immediately go and execute method1?

+5
source share
2 answers

It's like an interruption ...

No, absolutely not. There is a safe way to interrupt a thread when it is busy executing code. This causes a particularly unpleasant type of problem called a "re-login error." This is a mistake that firmware programmers struggle with when they implement interrupt handlers on embedded systems. Some information on this in this web page .

-, -. , . , . " ". , , , . , , .

Winforms , . , , , . Begin/Invoke invoke PostMessage(), , - .

, , , . , , GetMessage(). , . .

Invoke BeginInvoke Set() ManualResetEvent . , . , , , .

, , :

  • , , .
  • , BeginInvoke,
  • / , - , .
  • , BeginInvoke, , , , , . .
  • Invoke BeginInvoke . , , - . , - . BeginInvoke over Invoke.
  • , , . , . , . , .
  • , , . , , , . , ObjectDisposedException - .
+4

: method1 ( GUI). :

public void method1(Object sender, EventArgs args)
{
  if(dataGridView1.InvokeRequired)
    method1();
  else
    // Do something to dataGridView1
}

, , .

Control.Invoke.

MSDN, Invoke " , , ". "" : marshaler.

Invoke marshaler.MarshaledInvoke .

MarshaledInvoke , , (, Invoke, , , marshaler. syncSameThread.

, marshaler.

, syncSameThread - true, InvokeMarshaledCallbacks, ( marshaler).

+2

All Articles