Home All Groups Group Topic Archive Search About

Invoking code on a specific thread

Author
18 Mar 2005 12:06 AM
Phil Jones
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

Author
18 Mar 2005 1:15 AM
Leonid Finis
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
>
>
>
Author
18 Mar 2005 7:22 AM
Jon Skeet [C# MVP]
Phil Jones <phil_newsgr***@hotmail.com> 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??).

Well, it won't help you a lot...

> 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!)

Your other thread basically needs to be waiting to process things -
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
Author
18 Mar 2005 7:25 AM
Bruno Jouhier [MVP]
"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
> 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!)

You need a special kind of thread for this. The thread that creates the
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
>
Author
18 Mar 2005 9:49 PM
Phil Jones
Thanks everyone - this is queue concept is something I'll start figuring
out.  I appreciate all your insights.

====
Phil

AddThis Social Bookmark Button