call to the main thread
-
Hi all, I create a worker thread to do some stuff, when that thread completes I would like to make a call from that thread to main thread to tell it to take a new course of action. In a windows forms environment you can call: this.Invoke but what if your developing an assembly or console app , is there an equivalent. I've tried using delegates etc but when you look at the Threading.Thread.CurrentThread.Name property your still 'in' the worker thread. Anyone know about this. TIA.:)
-
Hi all, I create a worker thread to do some stuff, when that thread completes I would like to make a call from that thread to main thread to tell it to take a new course of action. In a windows forms environment you can call: this.Invoke but what if your developing an assembly or console app , is there an equivalent. I've tried using delegates etc but when you look at the Threading.Thread.CurrentThread.Name property your still 'in' the worker thread. Anyone know about this. TIA.:)
Are you trying to make sure that after something happens in your worker thread that a method is executed within the context of the main thread, or are you just trying to signal the main thread from your worker thread. Probably the easiest way would be to pass a waithandle to your worker thread and get your main thread to wait on the handle (or check it regularly) for the handle to be signaled and then go from there. But if thats the case then you might as well recode the worker thread as an asynchronous method call. The above may not be to your liking :) Looking at the Mono project, the way they have implemented System.Windows.Forms.Control.Invoke() is place the delegate passed ot Invoke() in a queue. This queue is then checked in the windows message loop (which executes in the same thread as the control was created in), and any delegates in the queue are then called. So you could create a synchronized queue, get your worker thread to place a delegate in this queue, and then either get your main thread to periodically check this queue or signal a waithandle, and then get the main thread to execute the delegates in the queue.
-
Are you trying to make sure that after something happens in your worker thread that a method is executed within the context of the main thread, or are you just trying to signal the main thread from your worker thread. Probably the easiest way would be to pass a waithandle to your worker thread and get your main thread to wait on the handle (or check it regularly) for the handle to be signaled and then go from there. But if thats the case then you might as well recode the worker thread as an asynchronous method call. The above may not be to your liking :) Looking at the Mono project, the way they have implemented System.Windows.Forms.Control.Invoke() is place the delegate passed ot Invoke() in a queue. This queue is then checked in the windows message loop (which executes in the same thread as the control was created in), and any delegates in the queue are then called. So you could create a synchronized queue, get your worker thread to place a delegate in this queue, and then either get your main thread to periodically check this queue or signal a waithandle, and then get the main thread to execute the delegates in the queue.
Hi Andy, I was looking to actually call a method on the main thread in the same way as Control.Invoke() 'marshals' the call to the main thread in a windows form. Your suggestions of passing a waithandle to the worker thread or using an asynchronous method call sound good and I will certainly look into them ( especially the async call, that hadn't occured to me at all :) ) Your description of how Mono works is very interesting and I imagine that microsoft do it in a similar way or at least thats how it appears to work. But it does seem to be a bit tricky for my feable brain and right now I need a quick and dirty solution. Thank you very much for help, it was very useful to me.
-
Are you trying to make sure that after something happens in your worker thread that a method is executed within the context of the main thread, or are you just trying to signal the main thread from your worker thread. Probably the easiest way would be to pass a waithandle to your worker thread and get your main thread to wait on the handle (or check it regularly) for the handle to be signaled and then go from there. But if thats the case then you might as well recode the worker thread as an asynchronous method call. The above may not be to your liking :) Looking at the Mono project, the way they have implemented System.Windows.Forms.Control.Invoke() is place the delegate passed ot Invoke() in a queue. This queue is then checked in the windows message loop (which executes in the same thread as the control was created in), and any delegates in the queue are then called. So you could create a synchronized queue, get your worker thread to place a delegate in this queue, and then either get your main thread to periodically check this queue or signal a waithandle, and then get the main thread to execute the delegates in the queue.
I am having the same problem. I am calling the FileWatcher component in the .NET framework from a server (middleware) class. If I place this on a form, I'm set, I just hand the FileWatcher class a reference to my form. The file watcher class then calls the form's Invoke method. However I don't have a form, so am forced to implement the ISynchronizeInvoke interface myself. Your suggestion to go look at the mono project seems like a good start. Can you tell me where to find this project? My solution so far is to implement ISnychronizeInvoke and to create an delegate instance that will be executed when the callback is executed. 1. I register a call back. 2. The call back is executed. The sender is the operating system. 3. I then execute a delegate that I instantiated on the "target" thread. David Minor Applications Programmer NC State Archives
-
I am having the same problem. I am calling the FileWatcher component in the .NET framework from a server (middleware) class. If I place this on a form, I'm set, I just hand the FileWatcher class a reference to my form. The file watcher class then calls the form's Invoke method. However I don't have a form, so am forced to implement the ISynchronizeInvoke interface myself. Your suggestion to go look at the mono project seems like a good start. Can you tell me where to find this project? My solution so far is to implement ISnychronizeInvoke and to create an delegate instance that will be executed when the callback is executed. 1. I register a call back. 2. The call back is executed. The sender is the operating system. 3. I then execute a delegate that I instantiated on the "target" thread. David Minor Applications Programmer NC State Archives
The mono project is located at www.go-mono.com[^] HTH Andy