BeginInvoke vs QueueUserWorkItem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a windows form of which I will be saving the user's selections from
DropDownBoxes, CheckBoxes, and RadioButtons. Once the choices are collected,
I really will have no use for any of the controls. Is there a benefit of
using Control.BeginInvoke opposed to ThreadPool.QueueWorkItem? From the
threads I have briefly scanned over on the www, there really is no difference
other. Some have mentioned that ThreadPool.QueueWorkItem maybe faster, but
thats about all. Any thoughts?
 
RWF,

The question one has to ask is, to what end are you doing this?

These things are actually very different.

Control.BeginInvoke/Invoke will cause the delegate passed to it to be
executed on the thread that calls need to be synchronized on. Windows
controls require changes to them to be executed on the thread that created
them. When you call BeginInvoke/Invoke, it makes sure that the call is
marshaled correctly.

When you call QueueWorkItem, a method is executed on another thread of
execution.

Now, when you call BeginInvoke, it will call Invoke on another thread of
execution, through QueueWorkItem, but the call ultimately will end up being
called on the thread that created the control. It's just that you don't
have to wait for Invoke to return on the thread you call BeginInvoke on.

Hope this helps.
 
Thanks Nicholas, that helps a bunch.

Nicholas Paldino said:
RWF,

The question one has to ask is, to what end are you doing this?

These things are actually very different.

Control.BeginInvoke/Invoke will cause the delegate passed to it to be
executed on the thread that calls need to be synchronized on. Windows
controls require changes to them to be executed on the thread that created
them. When you call BeginInvoke/Invoke, it makes sure that the call is
marshaled correctly.

When you call QueueWorkItem, a method is executed on another thread of
execution.

Now, when you call BeginInvoke, it will call Invoke on another thread of
execution, through QueueWorkItem, but the call ultimately will end up being
called on the thread that created the control. It's just that you don't
have to wait for Invoke to return on the thread you call BeginInvoke on.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

RWF said:
I have a windows form of which I will be saving the user's selections from
DropDownBoxes, CheckBoxes, and RadioButtons. Once the choices are
collected,
I really will have no use for any of the controls. Is there a benefit of
using Control.BeginInvoke opposed to ThreadPool.QueueWorkItem? From the
threads I have briefly scanned over on the www, there really is no
difference
other. Some have mentioned that ThreadPool.QueueWorkItem maybe faster,
but
thats about all. Any thoughts?
 
Now, when you call BeginInvoke, it will call Invoke on another thread
of execution, through QueueWorkItem, but the call ultimately will end up
being called on the thread that created the control. It's just that you
don't have to wait for Invoke to return on the thread you call BeginInvoke
on.

This is incorrect.

BeginInvoke uses the PostMessage API to put the message asynchronously to
the other thread. The PostMessage API queues the message and returns.. (so
in general you are queuing either way).

If I remember correctly, Invoke actually does the same thing but blocks
until it has a response although I believed it called SendMessage at some
point in history.

Just to be clear ...

Cheers,

Greg Young
MVP - C#
Nicholas Paldino said:
RWF,

The question one has to ask is, to what end are you doing this?

These things are actually very different.

Control.BeginInvoke/Invoke will cause the delegate passed to it to be
executed on the thread that calls need to be synchronized on. Windows
controls require changes to them to be executed on the thread that created
them. When you call BeginInvoke/Invoke, it makes sure that the call is
marshaled correctly.

When you call QueueWorkItem, a method is executed on another thread of
execution.

Now, when you call BeginInvoke, it will call Invoke on another thread
of execution, through QueueWorkItem, but the call ultimately will end up
being called on the thread that created the control. It's just that you
don't have to wait for Invoke to return on the thread you call BeginInvoke
on.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

RWF said:
I have a windows form of which I will be saving the user's selections from
DropDownBoxes, CheckBoxes, and RadioButtons. Once the choices are
collected,
I really will have no use for any of the controls. Is there a benefit of
using Control.BeginInvoke opposed to ThreadPool.QueueWorkItem? From the
threads I have briefly scanned over on the www, there really is no
difference
other. Some have mentioned that ThreadPool.QueueWorkItem maybe faster,
but
thats about all. Any thoughts?
 

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

Back
Top