T
Tony Johansson
Hello!
Here I have an example of a program that is using an additional worker
thread.
There is one thing here that is strange and that is how is it possible that
the worker thread
will finish it's work before the main thread if I enter 100 for the
interval.
As far as I know it's a heavy process to start a new Thread but here it
doesn't seem to take no time becuse the worker thread will fisish it's jobb
before the main thread.?
public class Test
{
static int interval;
static void DisplayNumbers()
{
Thread thisThread = Thread.CurrentThread;
string name = thisThread.Name;
Console.WriteLine("Starting thread: " + name);
Console.WriteLine(name + ": Current Culture = " +
thisThread.CurrentCulture);
for (int i = 1; i <= 8*interval; i++)
if (i % interval == 0) Console.WriteLine(name + ": count has
reached " + i);
}
static void StartMethod()
{
DisplayNumbers();
Console.WriteLine("Worker Thread Finished");
}
public static void Main()
{
Console.Write("interval to display results at?> ");
interval = int.Parse(Console.ReadLine());
Thread thisThread = Thread.CurrentThread;
thisThread.Name = "Main Thread";
ThreadStart workerStart = new ThreadStart(StartMethod);
Thread workerThread = new Thread(workerStart);
workerThread.Name = "Worker";
workerThread.Start();
DisplayNumbers();
Console.WriteLine("main Thread Finished");
Console.ReadLine();
}
}
Here I have an example of a program that is using an additional worker
thread.
There is one thing here that is strange and that is how is it possible that
the worker thread
will finish it's work before the main thread if I enter 100 for the
interval.
As far as I know it's a heavy process to start a new Thread but here it
doesn't seem to take no time becuse the worker thread will fisish it's jobb
before the main thread.?
public class Test
{
static int interval;
static void DisplayNumbers()
{
Thread thisThread = Thread.CurrentThread;
string name = thisThread.Name;
Console.WriteLine("Starting thread: " + name);
Console.WriteLine(name + ": Current Culture = " +
thisThread.CurrentCulture);
for (int i = 1; i <= 8*interval; i++)
if (i % interval == 0) Console.WriteLine(name + ": count has
reached " + i);
}
static void StartMethod()
{
DisplayNumbers();
Console.WriteLine("Worker Thread Finished");
}
public static void Main()
{
Console.Write("interval to display results at?> ");
interval = int.Parse(Console.ReadLine());
Thread thisThread = Thread.CurrentThread;
thisThread.Name = "Main Thread";
ThreadStart workerStart = new ThreadStart(StartMethod);
Thread workerThread = new Thread(workerStart);
workerThread.Name = "Worker";
workerThread.Start();
DisplayNumbers();
Console.WriteLine("main Thread Finished");
Console.ReadLine();
}
}