Library tutorials & articles
.NET Threading Part I
Join
Before .NET, I was often asked questions about how to wait for a Win32 thread
to exit. The solution was to acquire a handle to the thread and wait on the
handle. Or alternatively, you could setup an event that was triggered at the
end of the thread and wait on that event. .NET provides us with a simpler
method of doing the same. If you call the Thread.Join instance
method, then the current thread will wait until the thread represented by the
Thread object is terminated.
using System;
using System.Threading;
using System.Diagnostics;
namespace ConsoleApplication7
{
class Class1
{
public void Pump()
{
for (int i= 0; i< 100; i++)
{
Console.WriteLine("Value
{0}", i);
Thread.Sleep(1);
}
}
static void Main(string[] args)
{
Class1 obj = new Class1();
Thread pump = new Thread(
new ThreadStart(obj.Pump));
pump.Start();
Thread.Sleep(500); // force the other
thread
// thru a couple iterations
pump.Join(); // wait until the thread
is
// completed
Console.WriteLine("Goodbye");
}
}
}
In this previous listing, the main thread creates a new thread (pump), then
waits for the thread to complete by calling the pump.Join instance
method. If you run the previous code, as is, then the output will be the numbers
0 to 99 and finally the word Goodbye. If you remove the call to pump.Join,
then the Goodbye message may be printed before the last number. I chose to put
the main thread to sleep for half a second as this displayed the Goodbye message
in the middle of the stream of numbers (when pump.Join was removed).
Related articles
Related discussion
-
hey developers out there
by pitsophera (0 replies)
-
How can i develop opc server using .net?
by vairajaig (1 replies)
-
Creating a Windows Service in VB.NET
by davidvanr (108 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
An Introduction to VB.NET and Database Programming
by carlosmen (14 replies)
Related podcasts
-
A Practical Look at Silverlight 2 Part 1
Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...
Events coming up
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
I was asked to do the following:
write the method call to begin running the thread and begin processing
(a) namespace = testProject
(b) form to run = frmMain
All the things that I have read on threading so far say that a thread point to a function of whatever comes after the "addressOf" in the argument. Therefore, I don't understand what (a) and (b) are trying to refer to or specify. If you happen to understand what that question is looking for, or know any good literature I can look at for the given topic, please let me know. Thank you.
email me
Are you ensuring that the page doesn't finish loading before both threads have returned a result? Otherwise, you may find that ASP.NET is outputting the page to the client before the thread has actually called the callback!
Yes...and if I may add, rather hesitantly at this juncture, I did think about that.
What surprised me however was when I ran this in debugging mode, I as able to trace both callbacks
and in one case the dataset returned had no data. I wonder if there are any issues with returning non-simple types
in the event driven call back. May be, using the delegate mechanism may work. I havn't done
much callback based threading so far...
This thread is for discussions of .NET Threading Part I.