Control.BeginInvoke and execution order

N

Nobody

1) If I call Control.BeginInvoke() several times on a worker thread, are the
delegates guaranteed to execute in the order I submitted the request?

2) Control.BeginInvoke() does not have an "on complete" callback parameter,
therefore is EndInvoke() necessary? My understanding is that
Delegate.BeginInvoke() will leak data if I do not call Delegate.EndInvoke(),
is the same true for Control.BeginInvoke()?

Thanks
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,
1) If I call Control.BeginInvoke() several times on a worker thread, are the
delegates guaranteed to execute in the order I submitted the request?

I think there is no guarantee but I still assume that the answer is "Yes" as
the most likely way to marshal async calls to the UI thread is by posting
messages to its message queue.
2) Control.BeginInvoke() does not have an "on complete" callback parameter,
therefore is EndInvoke() necessary?

Not 100% sure but I don't think it's necessary unless the worker thread has
to wait for the async call to complete.
 
T

Tian Min Huang

Hi,

Thanks for your post. I'd like to share the following information with you:

1) If I call Control.BeginInvoke() several times on a worker thread, are
the delegates guaranteed to execute in the order I submitted the request?

Yes, because the Control.BeginInvoke will be marshaled to (executed on) the
control's creation thread when you call them in your worker thread. As you
know, Windows Forms uses the single-threaded apartment (STA) model because
Windows Forms is based on native Win32 windows that are inherently
apartment-threaded. The STA model implies that a window can be created on
any thread, but it cannot switch threads once created, and all function
calls to it must occur on its creation thread.

I believe the following articles are helpful:

Safe, Simple Multithreading in Windows Forms, Part 1
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms06112002.asp

Safe, Simple Multithreading in Windows Forms, Part 2
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms08162002.asp

2) Control.BeginInvoke() does not have an "on complete" callback parameter,
therefore is EndInvoke() necessary?

No, there is no need to call EndInvoke unless you need to get the return
value of the asynchronous operation.

Does this answer your questions?

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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