Solved

Thread.Join Method in C#

Posted on 2009-05-04
1
1,295 Views
Last Modified: 2012-05-06
Hi Experts,
Could any one explain me Thread.Join Method in C# with simple examples. I tried on google but could nt find any resource to clearly understand the method
thanks,
0
Comment
Question by:mmalik15
1 Comment
 
LVL 21

Accepted Solution

by:
Tapan Pattanaik earned 500 total points
ID: 24295233
Using Thread.Join  :

More often than not, you will need your application to wait for a thread to complete
execution. To accomplish this, the Thread class supports the Join method:

' VB
theThread.Join()

// C#
theThread.Join();

The Join method tells the system to make your application wait until the thread has
completed. Of course, in this simple case you do not really need a second thread
because you are just waiting for it to complete anyway. A better example is for us to
have five threads that all do some work and to wait for them. When we are working
with multiple threads, our programming task is a bit more complicated, as we need to
wait for all our threads. We can do this by keeping a reference to all our threads and
calling Join on each of the threads to wait for the threads to complete, one at a time,
as demonstrated in the following code:

// C#

ThreadStart operation = new ThreadStart(SomeWork);
Thread[] theThreads = new Thread[5];
for (int x = 0; x < 5; ++x)
{
// Creates, but does not start, a new thread
theThreads[x] = new Thread(operation);
// Starts the work on a new thread
theThreads[x].Start();
}
// Wait for each thread to complete
foreach (Thread t in theThreads)
{
t.Join();
}
By storing the threads in an array, we can wait for each of the Threads one at a time. As
each thread completes, the Join method will return and we can continue.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question