Creating and Starting Multiple Threads

In our first example, we'll create two new threads and use them to display text in a console application. One thread will type "No worries." 20 times, and the other will type "No Problems." 20 times. We start this new example, ch15_01.cs, by creating an object and calling a method named StartThreads from Main because we can't use only static methods in this example:

 
 static void Main() {   ch15_01 app = new ch15_01();   app.StartThreads(); } 

In the StartThreads method we'll create two new threads, thread1 and thread2 , using the Thread class's constructor. Threads operate on the method level in C#, which means you pass the name of the method that holds a thread's code using a ThreadStart delegate to the Thread constructor (those methods will be named NoWorries and NoProblems here). After you've created a new thread, you call the thread's Start method to start executing the code in the thread's method:

 
 public void StartThreads() {   Thread thread1 = new Thread(new ThreadStart(NoWorries));   Thread thread2 = new Thread(new ThreadStart(NoProblems));   thread1.Start();   thread2.Start(); } 

In the NoWorries method, we can print "No Worries." in the console window 20 times:

 
 public void NoWorries() {   for (int loopIndex = 0; loopIndex < 20; loopIndex++)   {     System.Console.WriteLine("No Worries.");   } } 

Similarly, in the NoProblems method, we can print "No Problems." in the console window 20 times, as you see in ch15_01.cs, Listing 15.1.

Listing 15.1 Creating Two Threads (ch15_01.cs)
 using System.Threading; class ch15_01 {   static void Main()   {     ch15_01 app = new ch15_01();     app.StartThreads();   }   public void StartThreads()   {     Thread thread1 = new Thread(new ThreadStart(NoWorries));     Thread thread2 = new Thread(new ThreadStart(NoProblems));     thread1.Start();     thread2.Start();   }   public void NoWorries()   {     for (int loopIndex = 0; loopIndex < 20; loopIndex++)     {       System.Console.WriteLine("No Worries.");     }   }  public void NoProblems()   {   for (int loopIndex = 0; loopIndex < 20; loopIndex++)   {   System.Console.WriteLine("No Problems.");   }   }  } 

Now when you run this example, the machine will give some time to thread1 and then some time to thread2 . Computer processing time is divided into slices , so thread1 will get some time, and then thread2 will, and then back to thread1 , and so on. Here's what you might see when you run this example:

 
 C:\>ch15_01 No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Problems. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. No Worries. 

As you can see, thread1 got some time to run, and then thread2 did, and then back to thread1 , and so on.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net