0

I want to execute one delegate on the main(UI) thread. However, I do not get that thread at run time due to a third-party plugin (ESRI). This plugin starts on receiving messages from a web application through a third-party DLL file in the form of XML. The strange thing is that, on receiving the message, further execution of the (ESRI) plugin starts on a different thread, say ID = 3.

When I run this third-party plugin standalone, (like adding button and on click of that button execute further code) it doesn't throw any exception. In this case, code executes in the main (UI) thread, like thread ID = 1.

Is there a way (in C#) to execute code on the required thread, say thread ID =1?

Note: As it is a third-party plugin, we do not get any control over any windows control/form. We can't even add a control through it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

0

You can use this to run any code in the UIThread

Deployment.Current.Dispatcher.BeginInvoke(() => { /* UI code here*/ });
Shumais Ul Haq
  • 1,427
  • 1
  • 15
  • 26
atomaras
  • 2,468
  • 2
  • 19
  • 28
  • atomaras, Thanks for the reply. However, as I mentioned earlier in the problem that though we call dispatcher for this method, it doesn't invoke on main thread, say ID=1. Instead, it starts with new thread and this thread starts upon receiving message from another web application. So in this case, I can manage to get that thread (main thread with ID = 1), however can not start further execution of the code on that specific thread. Looking for any help..... – vinayak jogade May 29 '13 at 05:24
  • i am guessing here because your description leaves a little room for confusion. I think what is happening is that your plugin is invoked from within the callback of some WebRequest that gets posted back to the ThreadPool instead of the UIThread. Sounds about right? Lets say the method that starts your plugin is called Initialize(). Then what you can do, if that method requires the UIThread, is automatically post the code inside the Initialize method on the UIThread using the code above. Example: public void Initialize() { Deployment.Current.Dispatcher.BeginInvoke(() => { /* init */ }); } – atomaras May 29 '13 at 07:22