Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Thread.Join Method in C#

Posted on 2009-05-04
1
Medium Priority
?
1,355 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 1500 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

971 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