473,654 Members | 3,098 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(prJ ob.Document);
prT = new Thread (new ThreadStart (prThread.Execu te));
prT.IsBackgroun d = 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 2345
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
"asynchrono us" 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.BeginInvok e(new dlgcallwhendone (this.callwhend one) );
}

here I'v declared the delegate "dlgcallwhendon e" 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******** *******@moratek nikutveckling.s e...
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(prJ ob.Document);
prT = new Thread (new ThreadStart (prThread.Execu te));
prT.IsBackgroun d = 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
ParameterizedTh readProc 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******** ******@moratekn ikutveckling.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
ParameterizedTh readProc 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******** *******@moratek nikutveckling.s e...
On Fri, 7 Oct 2005 16:36:47 -0700, Abubakar wrote:
For passing a parameter to thread you could also use the
ParameterizedTh readProc 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
4692
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 should work in netsacpe also. please advise me . regds, Navin Mahindroo
2
1370
by: Dayne | last post by:
Does an asychronous thread (.beginInvoke) die when the method call ends? DT
8
6083
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 uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
8
1460
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
2789
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 the problem is that sometimes the user still closing their browser window by clicking on the "x" button of the window. This is quite a problem for us because it will leave unreleased resource on the server for an unpredictable period of time,...
4
1173
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 this thread around for the lifetime of the object, reading and sending data to the from, but it seems to exit when the form completed loading. Why is it doing this? And is there a way I can keep it around after the form loads, without the form...
3
1079
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, one for System.Relefection.MethodBase.GetCurrentMethod(), and one for a Sql Data object (Data Adapter, Table Adapter, or SQL Command)... spawns a thread, saves info to a DB or dumps a strongly typed dataset to xml... no worries. BUT try {
27
2112
by: cmdolcet69 | last post by:
Can anyone tell me if its possible to do multiple event args in vb.net?
1
2844
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 viewer. My Waring is Event code: 3005 Event message: An unhandled exception has occurred. Event time: 8/14/2009 3:04:16 PM
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2714
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 we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.