Invoking code on a specific thread

P

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
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

Phil Jones said:
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.
 
B

Bruno Jouhier [MVP]

Phil Jones said:
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.
 
P

Phil Jones

Thanks everyone - this is queue concept is something I'll start figuring
out. I appreciate all your insights.

====
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top