Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I already did a post about threads. I am developing an application to fetch mails from the mail server.

The application will be running 24/7 without stopping. I am using a timer control and every 5 minutes the timer1_tick event is fired and the two threads are called.

What each thread is doing is that it is calling a connectpara. Each thread contains a set of users. Once the thread is called, it will fetch mails for that users.

C#
private void timer1_Tick(object sender, EventArgs e)
{
   Thread t = new Thread(connectPara);
   t.Start("sp_users1");
   textBox1.Text = t.ThreadState.ToString();           
   Thread t1 = new Thread(connectPara);
   t1.Start("sp_users2");
   textBox2.Text = t1.ThreadState.ToString();
}


I am using the thread for the first time and my doubt is:

In this context, if each thread is executed, how do I know that the thread has finished the job?

If finished, do I need to abort or dispose the thread just like we do for objects.

Kindly help me to resolve this.

Thanks & regards
Posted
Updated 8-Dec-09 14:23pm
v5

wrote:
How do i know that the thread has finished the job?


As I said before, you can call Thread.IsAlive or Thread.ThreadState ( which gives more information ).


wrote:
Do i need to abort or dispose the thread just like we do for objects?


Thread does not implement IDisposable, so you can't dispose it.

Thread.Abort is evil - never use it!

So the answer to your question is no. When a thread finishes, just remove any references to it and the runtime will clean up for you.



I suggest that you keep references to your threads in your control class and when the timer fires, check to see if the previous threads have completed. If they are still working then just return and wait for the next timer. If they have finished, then you can create new ones again.

Nick
 
Share this answer
 
The common practice and the easiest way is to call thread.Join() for each new thread in cycle after your threads have launched. The main thread will wait for the "joined" threads to be finished and then you can notify a user.



You also can signal threads explicitly via ManualResetEvent

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900