Can one specify the thread that the AsyncCallback runs on when using delegate.BeginInvoke?
.net, c#, multithreading
Solution
On the thread you want to use as the target for your async operation, the CurrentDispatcher Property is a System.Threading.Dispatcher object that can be used to force a callback to execute on that thread.
This is the base class that the Control class uses to implement BeginInvoke.
Questions have come up about using this with Windows forms. I don't think this will be a problem, although if you have a form, then form.BeginInvoke is a better choice. It appears that both the form and WPF use the same base class for handling invoke. http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx
Problem
From my understanding of .NET, if I use a BackgroundWorker and have an event handler for RunWorkerCompleted, the event handler will run on the same thread in which RunWorkerAsync was called. If instead I use BeginInvoke on a delegate to run a method asynchronously, and pass an AsyncCallback parameter to BeginInvoke, is there any way I can specify that the callback runs on the same thread that called BeginInvoke -- or for that matter any arbitrary thread? From my understanding the callback runs on the next available thread from the thread pool. That's fine, but is there a way I can run code in an AsyncCallback on any thread I want? I do know you can use BeginInvoke on a form or control and make code within the callback run on the thread that created the UI element. But what about if I want to run code on a non-UI thread with no forms or controls?