Event when a thread ends?

  • Thread starter Thread starter Anders Eriksson
  • Start date Start date
A

Anders Eriksson

Hello!

I have a form from which I start an thread running in the background and I
want to have an event when the thread is done/ends.

I start the thread like this:
private Thread prT; // this is a member of the Form1 class

// in a Form1 member function I do:
PrintThread prThread = new PrintThread(prJob.Document);
prT = new Thread (new ThreadStart (prThread.Execute));
prT.IsBackground = true;
prT.Start();


How do I create an event when the thread ends?

// Anders
 
For passing a parameter to thread you could also use the
ParameterizedThreadProc delegate if you are using the .net 2.0. But thats ok
to keep it in the class if you have earlier versions.

Since you are passing the prJob.Document to your class, why dont you pass a
Form1 reference to it also and than replace the "this" with form reference
variable in the code I wrote for you. I think this will solve your problem.
This worked perfectly!

Now I have to read up on delegates and threads and ....

Since the program works now I of cause will have much time to do the
reading ;-)

Thank you very much!

// Anders
 
Abubakar... I don't know how or why, but your posts are being
timestamped several hours into the future.

This results in all threads to which you post "hanging" at the top of
the "most recent" list for several hours.

Is there anything you can fix on your PC so that your timestamps will
be correct?
 
Hi,
assume threadproc is your thread proc:

void threadproc()
{
// add thread execution logic here

/* NOTIFICATION PART: This is important. Assuming that you r writing this
code inside a windows form class.
Than "this" would refer to windows form itself. I'm going to make an
"asynchronous" call to a method of the form class so that it knows that the
thread is finished execution and the rest of the logic based on the thread's
results can be started. Note also that the call to callwhendone() function
below will
execute in the "thread of the form", not this thread which initiates the
asynchronous call.
*/

this.BeginInvoke(new dlgcallwhendone (this.callwhendone) );
}

here I'v declared the delegate "dlgcallwhendone" with callwhendone()
signatures which is our custom method here.

delegate void dlgcallwhendone();

void callwhendone()
{
MessageBox.Show ( "Thread is done executing.");


}

I hope this helps.

Ab.
http://joehacker.blogspot.com
 
Since I need to pass some data into the threadproc I need to have it in a
class (?). So I can't use this. since it will not point to the form but to
the class...

For passing a parameter to thread you could also use the
ParameterizedThreadProc delegate if you are using the .net 2.0. But thats ok
to keep it in the class if you have earlier versions.

Since you are passing the prJob.Document to your class, why dont you pass a
Form1 reference to it also and than replace the "this" with form reference
variable in the code I wrote for you. I think this will solve your problem.

Ab.
http://joehacker.blogspot.com
 

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

Stopping a listening thread 2
ThreadPool Questions 11
threading 2
Help with thread 1
Returning value from MT - need help 5
Thread 1
Threads and the OleDbDataReader Class 3
Thread GC collection 6

Back
Top