I once met the opportunity to execute the code, wedged into the running Thread . But I can not remember how it was done. The essence is the same as for Dispatcher.Invoke() or Dispatcher.BeginInvoke() , but for Thread
- And what does it mean to "execute code wedged into a running thread"? - Vladimir Martyanov
- @Vladimir Martianov, I gave you an example based on the Dispatcher. That is, I need to perform my Action in the already existing and working thread, with the ability to execute immediately or wait until the thread finishes the current task. - iRumba
- For reference, "wedge in" can only be in a thread that has a message queue. - MSDN.WhiteKnight
- stackoverflow.com/questions/4843010/… - kmv
|
1 answer
The Init() method will not complete until BeginInvokeShutdown(); is called BeginInvokeShutdown();
private void Init() { Thread workerThread = new Thread(new ParameterizedThreadStart(DoWork)); workerThread.IsBackground = true; workerThread.Start(Dispatcher.Current); Dispatcher.Run(); // Once shutdown you cannot restart the dispatcher in this appdomain } void DoWork(object startArg) { Dispatcher targetDispatcher = startArg as Dispatcher; if(targetDispatcher == null) { // Log error return; } while(working) { targetDispatcher.BeginInvoke(....); } targetDispatcher.BeginInvokeShutdown(...); } |