Thread A calls a delegate on Thread B but Thread A executes it!?!?

P

Paul Tomlinson

OK, I have a multi-threaded application.
I have a master thread which calls a delegate which I want the child thread
to execute but that is not happening, instead the master thread is running
the code in the delegate function. I know this as I can see the thread
id's.

This is what I *want* to happen

i.e.
(Master Thread:)
this.Invoke( DelegateA )

.....

(Child Thread)
private void DelegateFunction()
{
....
}


Is there anyway I can get this to happen?
Ta.
 
J

Jon Skeet [C# MVP]

Paul Tomlinson said:
OK, I have a multi-threaded application.
I have a master thread which calls a delegate which I want the child thread
to execute but that is not happening, instead the master thread is running
the code in the delegate function. I know this as I can see the thread
id's.

This is what I *want* to happen

i.e.
(Master Thread:)
this.Invoke( DelegateA )

....

(Child Thread)
private void DelegateFunction()
{
...
}


Is there anyway I can get this to happen?

Thread B needs to be waiting to receive the work in some form or other.
I have an example of something like this with the producer/consumer
queue in my threading article:
http://www.pobox.com/~skeet/csharp/threads/
 
G

Guest

Try doing a DelegateA.BeginInvoke instead of an Invoke. And realize that you
will either need to create a callback method that handle the completion of
the process by calling an EndInvoke, just to clean things up.
 
J

Jon Skeet [C# MVP]

<"=?Utf-8?B?QnJ1Y2UgSm9obnNvbiBbQyMgTVZQXQ==?=" <Bruce Johnson [C#
MVP]@discussions.microsoft.com> said:
Try doing a DelegateA.BeginInvoke instead of an Invoke. And realize that you
will either need to create a callback method that handle the completion of
the process by calling an EndInvoke, just to clean things up.

That won't call it on a particular thread though - it'll call it on a
threadpool thread. That may be okay, but it may not be.
 
W

Willy Denoyette [MVP]

Just currious but, why do you want to execute a method on a specific thread?

Willy.
 

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