473,513 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event when a thread ends?

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
6 2337
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
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4690
by: Navin | last post by:
which event is fired when a browser is closed. i want to call asp page which deleet some files on the server. i tired using onunload event but it aslo get fired when a page is refreshed. the thing...
2
1357
by: Dayne | last post by:
Does an asychronous thread (.beginInvoke) die when the method call ends? DT
8
6075
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
8
1454
by: Marek | last post by:
Hi, Is Application_OnStart event hadler guaranteed to finish its execution before another HttpApplication object is brought into life? Marek
1
2780
by: Feng | last post by:
Hi, Need help on the following issue. We need to clean up some session specific backend resource when a user ends his session. We have a "Logoff" button on the pages that handles that. But...
4
1167
by: DaTurk | last post by:
Hi, I have this windows app that I'm working on. When it loads it instantiates this object which creates a thread to read data and send it to the form. My problem is, is that I want to keep...
3
1070
by: CrashTECH | last post by:
Okay, I have NO idea if this is possible (or a good idea) but I am getting REALLY annoyed... So I wrote this error logging class, to specs. It takes 3 objects as parameters, one for the exception,...
27
2087
by: cmdolcet69 | last post by:
Can anyone tell me if its possible to do multiple event args in vb.net?
1
2838
by: anithaapr05 | last post by:
I have got the warning in event viewer when i wrote the code in global.asax session_end(). When the user sessions time out, the Session_End event fires successfully.But i got the waring in event...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.