invoking a method on a particular thread

G

gkellymail

In general what I would like to do is have an application with X number
of threads and then be able to call a method and have it execute in the
thread of my choice. What would be the best way to accomplish this?

One reason I want to do this is that I'm working on a asp.net
application. This app will use an acitveX control that will generate
map images for me. This activeX control also must run in a thread with
appartment state STA. So, after this thread creates an instance of
this activeX control I would like it to "sleep" and wait to be told
what to do.

I could use events but I would rather be able to simply instruct the
"sleeping" thread to execute the method of my choice and pass
parameters as you normally word a method.

any ideas?
 
J

Jon Skeet [C# MVP]

In general what I would like to do is have an application with X number
of threads and then be able to call a method and have it execute in the
thread of my choice. What would be the best way to accomplish this?

All of the threads *have* to know that you want to do this, and have to
either poll a queue of "things to do" or actively wait on such a queue
(in the same way that the UI effectively waits on the message queue).
One reason I want to do this is that I'm working on a asp.net
application. This app will use an acitveX control that will generate
map images for me. This activeX control also must run in a thread with
appartment state STA. So, after this thread creates an instance of
this activeX control I would like it to "sleep" and wait to be told
what to do.

Right. That's basically a producer/consumer thread. See
http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml
half-way down the page. You'll need to pass an object which contains
all the information you need. In many ways, this would be like the
information stored for a custom threadpool work item - see
http://www.pobox.com/~skeet/csharp/miscutil
for the code to such a threadpool. (The threadpool itself might not be
useful to you, but the bit that takes the parameters and invokes the
method might. See MiscUtil.Threading.ThreadPoolWorkItem.)
 

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