looking for the method like postmessage in win32.

M

momonga

hi everyone.

i'm working on vs2003 and C#.
it is windows application that i'm creating.
so i have a form.

i created worker-thread and the worker thread calls back the callback-
method in the form.
in the callback-method, i'm going to implement the code that adds a
TabPage object to TabControl that's created by main-thread.

so i receive the exception that says "dont add the control to another
they are created by the diffrent thread." or something like that.

i need something like the PostMessage in win32.
to order the main-thread to perform the job on befalf of the worker-
thread.

please could you tell me how to do that?

thanks.
 
A

Alberto Poblacion

momonga said:
i need something like the PostMessage in win32.
to order the main-thread to perform the job on befalf of the worker-
thread.

The closest equivalent would be the Post method of the
SynchronizationContext. In your main thread you would do something like
ctx=SynchronizationContext.Current; then, in the other thread, you could do
ctx.Post(callback, arguments); The callback would then be executed in the
main thread.
please could you tell me how to do that?

There is another, possibly simple way. Call the Invoke method of any
Control (including the form) by passing a callback delegate. The callback
method will be invoked in the main thread.

tabControl1.Invoke(new MethodInvoker(AddPage));
....
pruvate void AddPage()
{
tabControl1.Pages.Add(....);
}
 
P

Peter Duniho

Alberto said:
The closest equivalent would be the Post method of the
SynchronizationContext. In your main thread you would do something like
ctx=SynchronizationContext.Current; then, in the other thread, you
could do ctx.Post(callback, arguments); The callback would then be
executed in the main thread.


There is another, possibly simple way. Call the Invoke method of any
Control (including the form) by passing a callback delegate. The
callback method will be invoked in the main thread.

I agree with your suggestion to use Invoke().

However, note that the Control.BeginInvoke() method is basically the
same as the SynchronizationContext.Post() method is basically the same
as the unmanaged PostMessage() function, in that all three are a "queue
and continue" operation, while Invoke(), Send(), and SendMessage() are
all a "queue and wait" operation.

If the OP really wants "queue and continue" semantics, they should use
Control.BeginInvoke() instead.

Pete
 
D

DaveB

I agree with your suggestion to use Invoke().

However, note that the Control.BeginInvoke() method is basically the
same as the SynchronizationContext.Post() method is basically the same
as the unmanaged PostMessage() function, in that all three are a "queue
and continue" operation, while Invoke(), Send(), and SendMessage() are
all a "queue and wait" operation.

If the OP really wants "queue and continue" semantics, they should use
Control.BeginInvoke() instead.

Pete- Hide quoted text -

- Show quoted text -

There's some more explanation and background in these posts that might
help.
http://davebrooks.wordpress.com/201...ontext-its-all-smoke-mirrors-and-postmessage/
http://davebrooks.wordpress.com/2007/02/12/begininvoke-the-land-of-confusion/

Dave
 

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