"Posting" a method call to the UI thread

E

Eric Dan

Hi,

Sometimes while handling an event in the UI thread I need to call some
action but I don't want it to be invoked in the context of the event (some
reentrant function calls may fail for example) but right after (in the same
UI thread) , something like PostMessage to the UI thread.

Is there an easy way in .Net / Windows Forms to "Post" a method call to the
UI thread?

Naturally, "Control.Invoke" will not help since it will identify that it is
the same thread and will execute immidiately. "BeginInvoke" is bad since it
will try to invoke on another thread.
 
M

Mattias Sjögren

"BeginInvoke" is bad since it will try to invoke on another thread.

What makes you say that?


Mattias
 
E

Eric Dan

My mistake.

The "Control" implementation of "BeginInvoke" is actually the solution. It
is designed to work on the same Control thread.
 
B

Brian Gideon

My mistake.

The "Control" implementation of "BeginInvoke" is actually the solution. It
is designed to work on the same Control thread.

Yep, the difference is that Invoke queues a message on the message
pump thread to run the delegate and *waits* for it to be processed
whereas BeginInvoke returns immediately.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top