Threading question!

  • Thread starter Thread starter Atmapuri
  • Start date Start date
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
 
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
 
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
 
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.
 
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
 
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
 
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.
 
Back
Top