Threading question!

A

Atmapuri

Hi!

How can I have a thread notify the main thread that it has ended?
The thread object seems to have only one event, ThreadStart.

Thanks!
Atmapuri
 
J

Jon Skeet [C# MVP]

How can I have a thread notify the main thread that it has ended?
The thread object seems to have only one event, ThreadStart.

Is the main thread a UI thread? If so, use Control.Invoke on one of
the UI objects "running in" the main thread.

If not, please give more information about your situation.

Jon
 
A

Atmapuri

Hi!
Is the main thread a UI thread? If so, use Control.Invoke on one of
the UI objects "running in" the main thread.

That would work, but I am still looking how to pass a function address
to the Invoke method.

Thanks!
Atmapuri
 
A

Alberto Poblacion

Atmapuri said:
How can I have a thread notify the main thread that it has ended?
The thread object seems to have only one event, ThreadStart.

I think that this is not correctly expressed. What you mean is "notify
the main program" instead of "notify the main thread". Just write a method
in your main program called "ThreadEnded" and call ThreadEnded() from the
code executing your separate thread. This method will, of course, execute in
the calling thread, not in the main thread. If the program is a WinForm and
you need to marshall execution to the main thread in order to update the
user interface, you can use the Invoke method of any Control (including the
Form) to enqueue a call to a method that will execute in the Control's
thread.
 
A

Atmapuri

Hi!

I get a compiler error message doing that:

Error 3 Argument '1': cannot convert from 'method group' to
'System.Delegate'

I have declared:

public void MyCallback()
{

}

and call it like this:

MyMainForm.Invoke(MyCallback);

What am I doing wrong here?

Thanks!
Atmapuri
 
J

Jon Skeet [C# MVP]

I get a compiler error message doing that:

Error 3 Argument '1': cannot convert from 'method group' to
'System.Delegate'

I have declared:

public void MyCallback()
{

}

and call it like this:

MyMainForm.Invoke(MyCallback);

What am I doing wrong here?

You're not specifying which type of delegate you're trying to convert
MyCallback into.

If you have:

delegate void Action();

then you can do:

MyMainForm.Invoke((Action)MyCallback);

Jon
 
P

Peter Ritchie [C# MVP]

If you're trying to execute code outside the GUI thread but the GUI thread
needs to know when the execution is complete, then I second what Roman said:
use BackgroundWorker. It removes the concern of the from from the background
logic and replaces it with a Completed event that the form subscribes to.
The BackgroundWorker Completed event ensures the thread that was used to
subscribe to the event is the same thread used to invoke the Completed event
(as well as the Progress event). As it stands now, you have a circular
dependence on the background code and the form.
 

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

Similar Threads

threading 2
deadlock 2
Is this a BackgroundWorker? 3
Thread GC collection 6
Works just as good without the Threadstart 3
Thread 1
Design Template for multithreading (an example here) 1
Sending messages... 3

Top