|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Invoking code on a specific threada normal object that is not a control. The functionality is: Control.Invoke(delegate) [Method] Control.InvokeRequired [Prop] I have a situation where I want to have the option to force execution on the original thread that the object was created with. I figure I can keep a reference to the "CurrentThread" within the constructor of the object (is that a bad idea??). --------- What I'm not sure about is how to implement the "Invoke" method functionality. That is, execute the specified delegate on the object's original thread (that was stored as a ref upon creation). How can this be done? Perhaps I'm missing something easy here??? (I hope so!) Thanks for any advice. Cheers everyone. === Phil : New Zealand In order to invoke a function on a particular thread, that thread has, first
of all, to be alive. It is very possible that the thread is already terminated. Second of all the thread is an "active entity" that can only be nofied in some way to execute a function. It can be notified for example through a queue of delegates that it is constantly scanning to execute. Keeping ThreadID will be not very usefull. Leonid Finis, MCSD.NET Show quote "Phil Jones" wrote: > I'm trying to re-create the thread functionality found on a user Control for > a normal object that is not a control. The functionality is: > > Control.Invoke(delegate) [Method] > Control.InvokeRequired [Prop] > > I have a situation where I want to have the option to force execution on the > original thread that the object was created with. > > I figure I can keep a reference to the "CurrentThread" within the > constructor of the object (is that a bad idea??). > > --------- > > What I'm not sure about is how to implement the "Invoke" method > functionality. That is, execute the specified delegate on the object's > original thread (that was stored as a ref upon creation). > > How can this be done? Perhaps I'm missing something easy here??? (I hope > so!) > > Thanks for any advice. Cheers everyone. > === > Phil : New Zealand > > > Phil Jones <phil_newsgr***@hotmail.com> wrote:
> I'm trying to re-create the thread functionality found on a user Control for Well, it won't help you a lot...> a normal object that is not a control. The functionality is: > > Control.Invoke(delegate) [Method] > Control.InvokeRequired [Prop] > > I have a situation where I want to have the option to force execution on the > original thread that the object was created with. > > I figure I can keep a reference to the "CurrentThread" within the > constructor of the object (is that a bad idea??). > What I'm not sure about is how to implement the "Invoke" method Your other thread basically needs to be waiting to process things - > functionality. That is, execute the specified delegate on the object's > original thread (that was stored as a ref upon creation). > > How can this be done? Perhaps I'm missing something easy here??? (I hope > so!) e.g. waiting on a producer/consumer queue. See http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml (half way down the page) for an example. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too "Phil Jones" <phil_newsgr***@hotmail.com> a écrit dans le message de news: uYqHjw0KFHA.***@TK2MSFTNGP10.phx.gbl...Show quote > I'm trying to re-create the thread functionality found on a user Control You need a special kind of thread for this. The thread that creates the > for a normal object that is not a control. The functionality is: > > Control.Invoke(delegate) [Method] > Control.InvokeRequired [Prop] > > I have a situation where I want to have the option to force execution on > the original thread that the object was created with. > > I figure I can keep a reference to the "CurrentThread" within the > constructor of the object (is that a bad idea??). > > --------- > > What I'm not sure about is how to implement the "Invoke" method > functionality. That is, execute the specified delegate on the object's > original thread (that was stored as a ref upon creation). > > How can this be done? Perhaps I'm missing something easy here??? (I hope > so!) objects must be designed so that it pumps messages from a queue and executes them. So, your Invoke method will allocate a delegate and queue it. Then, the other thread (the one that created the object) will dequeue the delegate and execute it. If you want the call to look synchronous from the caller's standpoint, you will need to setup your queue so that the invoking thread waits until the other thread has finished executing the delegate before continuing. But if you are ok with asynchronous execution (BeginInvoke), you don't need to wait. Hope this helps. Bruno. Show quote > > Thanks for any advice. Cheers everyone. > === > Phil : New Zealand > |
|||||||||||||||||||||||