By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

Event when a thread ends?

P: n/a
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
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
"Anders Eriksson" <an*****@gmail.com> wrote in message
news:wg***************@morateknikutveckling.se...
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
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

Nov 17 '05 #2

P: n/a
On Fri, 7 Oct 2005 15:41:29 -0700, Abubakar wrote:
/* NOTIFICATION PART: This is important. Assuming that you r writing this
code inside a windows form class.


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...

Is there a way of solving this?

// Anders
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation
Nov 17 '05 #3

P: n/a
> 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
"Anders Eriksson" <an*****@gmail.com> wrote in message
news:5g**************@morateknikutveckling.se... On Fri, 7 Oct 2005 15:41:29 -0700, Abubakar wrote:
/* NOTIFICATION PART: This is important. Assuming that you r writing this code inside a windows form class.


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...

Is there a way of solving this?

// Anders
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

Nov 17 '05 #4

P: n/a
On Fri, 7 Oct 2005 16:36:47 -0700, Abubakar wrote:
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

--
English isn't my first, or second, language.
So anything rude or strange are due to the translation
Nov 17 '05 #5

P: n/a
You r most welcome!

Ab.
http://joehacker.blogspot.com
"Anders Eriksson" <an*****@gmail.com> wrote in message
news:1a***************@morateknikutveckling.se...
On Fri, 7 Oct 2005 16:36:47 -0700, Abubakar wrote:
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

--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

Nov 17 '05 #6

P: n/a
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?

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.