Please see my comments to the question.
One approach alternative to the
BackgroundWorker
is using preliminary created thread, created through the constructor, which you can keep sleeping (wasting no CPU time for that, of course) and activate when the task is fed to it. This is a really good and stable variant of design, because you allocate all related resources only in the very beginning of runtime. Such
throttling is done by the class
System.Threading.ManualResetEvent
:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx[
^].
You just reset or signal the event thread handle, and the thread calling its method
WaitOne
is put to the wait state of the event handle is non-signaled. The thread will be awaken when it is signaled or by some other conditions (abort or timeout).
One more specialized form of such behavior with zero-waste CPU can be achieved with the
blocking queue. Please see my article on the topic:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation.
As you can see, one kind of items you can feed to the queue could be
delegate instances. This way, you can immediately feed tasks to the thread, and the thread will be waiting for the task, wasting no CPU time in the wait state. (And this is possible because threading is done based on
interrupts.)
By the way, this is how invocation of the delegate instances to some UI thread is working with
Dispatcher
, which brings us to other problem: how to work with UI from some non-UI thread?
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
Now, I promised to suggest thread wrapper and motivate it. Please read my past answers:
Thread wrapper,
Thread wrapper with lock: multithreading use,
Thread wrapper with event.
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
—SA