Execute method in another thread

  • Thread starter Thread starter Dierk Droth
  • Start date Start date
D

Dierk Droth

Hi,

I have 2 threads. Is there any way to execute a method
called vom thread 1 in the thread context of thread 2?

I'm aware that Control.Invoke excutes a callback in the
control's thread. But there is no control in my code :-((.
Any other solution ?

Strange, I couldn't find anything in all the docs I
browsed. I'm sure I did miss the point ?!?!?

Assistance is highly appreciated.

Dierk
 
Hi Dierk,

You will have to notify the thread2 to invoke it somehow (there are plenty
of sync mechanisms)
 
Hi Miha,

Thanx for your immediate reply.

Your suggestion probably would show me the right path.
But ... my problem is: thread 2 is not waiting e.g. on any
ressource. It performs some others tasks (or not) and is
out of my control. It should execute the desired method
when it starts being idle.

So, I'm still looking for some way to notify thread 2 that
it should execute a specific method.

Dierk
 
Hi Dierk,

Create a queue somewhere that both threads will see.
From thread1 place a request in queue.
When thread2 is idle, it should process the queue.
BTW, pay attention to queue access synchronization....
 
Dierk Droth said:
Your suggestion probably would show me the right path.
But ... my problem is: thread 2 is not waiting e.g. on any
ressource. It performs some others tasks (or not) and is
out of my control. It should execute the desired method
when it starts being idle.

So, I'm still looking for some way to notify thread 2 that
it should execute a specific method.

You can't just tell another thread to execute something - the thread
has to be actively looking for things to execute.
 
Jon Skeet said:
You can't just tell another thread to execute something - the thread
has to be actively looking for things to execute.

Do something like this:

struct ExecInfo
{
delegate Method;
object []args;
}

static ArrayList funcs;
void MyThreadFunc()
{
ExecInfo ei;
while(DoFuncs)
{
ei = null;
lock(funcs)
{
if(funcs.Count>0)
{
ei=funcs[0];
funcs.Remove(0);
}
}
if(ei==null)
{
Thread.Sleep(500);
break;
}
ei.Method.DynamicallyInvoke(ei.args);
}
}

This should take care of your problem.
You should probably use a shared data object, thought, to avoid race
conditions.
 
Ayende Rahien said:
Do something like this:

<snip>

That doesn't fit in with "thread 2 is [...] out of my control". (There
are better ways of accomplishing that, by the way - you'd use
Monitor.Wait/Pulse or Auto/ManualResetEvents.)
 
Jon Skeet said:
Ayende Rahien said:
Do something like this:

<snip>

That doesn't fit in with "thread 2 is [...] out of my control". (There
are better ways of accomplishing that, by the way - you'd use
Monitor.Wait/Pulse or Auto/ManualResetEvents.)

Then create you own thread.
And yes, I know about those methods, I'm using this because I didn't bother
to look up the descriptions for the above.
 
Ayende Rahien said:
Then create you own thread.

Given the rest of the posts, I'm assuming that the OP wants the method
to execute in one particular thread, not just any new thread.
And yes, I know about those methods, I'm using this because I didn't bother
to look up the descriptions for the above.

Well, so long as you don't mind burning CPU for no good reason...
 
Guys,

Wow, your answers are quite impressive. I never expected
such a great response.

1) I've looked the last days on the ManualResetEvent and
lock/Monitor.Wait/Pulse concepts; I'm just starting to
understand them. They will help me at another place in my
program.

2) I "solved" my problem on creating a
System.Windows.Forms.Control and it's handle and then
calling "Invoke". I've to admit: I didn't find another
way... :-((

3) Background
I'm writing an API using an asynchronous socket client and
my problem is: I want to have the replies (in fact calling
a delegate) executed in the same thread as the request.
The request thread might be an UI thread (I don't know,
since I'm writing an API), and the replies should be
executed in the same thread. I can't block this thread,
since it probably will be used by the caller for some
other stuff.

I hope this clearifies my request a bit more.

Dierk
 
Back
Top