Csharp/C Sharp/Thread/Thread Creation

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

An alternate way to start a thread

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// An alternate way to start a thread.

using System; using System.Threading;

class MyThread {

 public int count; 
 public Thread thrd; 

 public MyThread(string name) { 
   count = 0; 
   thrd = new Thread(new ThreadStart(this.run)); 
   thrd.Name = name; // set the name of the thread 
   thrd.Start(); // start the thread 
 } 

 // Entry point of thread. 
 void run() { 
   Console.WriteLine(thrd.Name + " starting."); 

   do { 
     Thread.Sleep(500); 
     Console.WriteLine("In " + thrd.Name + 
                       ", count is " + count); 
     count++; 
   } while(count < 10); 

   Console.WriteLine(thrd.Name + " terminating."); 
 } 

}

public class MultiThreadImproved {

 public static void Main() { 
   Console.WriteLine("Main thread starting."); 

   // First, construct a MyThread object. 
   MyThread mt = new MyThread("Child #1"); 

   do { 
     Console.Write("."); 
     Thread.Sleep(100); 
   } while (mt.count != 10); 

   Console.WriteLine("Main thread ending."); 
 } 

}


      </source>


Create a thread of execution

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Create a thread of execution.

using System; using System.Threading;

class MyThread {

 public int count; 
 string thrdName; 

 public MyThread(string name) { 
   count = 0; 
   thrdName = name; 
 } 

 // Entry point of thread. 
 public void run() { 
   Console.WriteLine(thrdName + " starting."); 

   do { 
     Thread.Sleep(500); 
     Console.WriteLine("In " + thrdName + 
                       ", count is " + count); 
     count++; 
   } while(count < 10); 

   Console.WriteLine(thrdName + " terminating."); 
 } 

}

public class MultiThread {

 public static void Main() { 
   Console.WriteLine("Main thread starting."); 

   // First, construct a MyThread object. 
   MyThread mt = new MyThread("Child #1"); 

   // Next, construct a thread from that object. 
   Thread newThrd = new Thread(new ThreadStart(mt.run)); 

   // Finally, start execution of the thread. 
   newThrd.Start(); 

   do { 
     Console.Write("."); 
     Thread.Sleep(100); 
   } while (mt.count != 10); 

   Console.WriteLine("Main thread ending."); 
 } 

}


      </source>


Create multiple threads of execution

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Create multiple threads of execution.

using System; using System.Threading;

class MyThread {

 public int count; 
 public Thread thrd; 

 public MyThread(string name) { 
   count = 0; 
   thrd = new Thread(new ThreadStart(this.run)); 
   thrd.Name = name; 
   thrd.Start(); 
 } 

 // Entry point of thread. 
 void run() { 
   Console.WriteLine(thrd.Name + " starting."); 

   do { 
     Thread.Sleep(500); 
     Console.WriteLine("In " + thrd.Name + 
                       ", count is " + count); 
     count++; 
   } while(count < 10); 

   Console.WriteLine(thrd.Name + " terminating."); 
 } 

}

public class MoreThreads {

 public static void Main() { 
   Console.WriteLine("Main thread starting."); 

   // Construct three threads. 
   MyThread mt1 = new MyThread("Child #1"); 
   MyThread mt2 = new MyThread("Child #2"); 
   MyThread mt3 = new MyThread("Child #3"); 

   do { 
     Console.Write("."); 
     Thread.Sleep(100); 
   } while (mt1.count < 10 || 
            mt2.count < 10 || 
            mt3.count < 10); 

   Console.WriteLine("Main thread ending."); 
 } 

}


      </source>


Creating Threads

<source lang="csharp"> /*

* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/

using System; using System.Threading; namespace Client.Chapter_15___Threading {

 public class CreatingThreads
 {
   static void Main(string[] args)
   {
     Thread MyNewThread = new Thread(new ThreadStart(ThreadProc));
     MyNewThread.Start();
     MyNewThread.Join();
   }
   protected static void ThreadProc()
   {
     for (int i = 0; i < 100; i++)
     {
       Console.WriteLine(i);
     }
   }
 }

}

      </source>


illustrates the creation of threads

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example14_1.cs illustrates the creation of threads
  • /

using System; using System.Threading; public class Example14_1 {

  // the Countdown method counts down from 1000 to 1
  public static void Countdown() {
   for (int counter = 1000; counter > 0; counter--) {
     Console.Write(counter.ToString() + " ");
   }
 }
 public static void Main() {
   // create a second thread
   Thread t2 = new Thread(new ThreadStart(Countdown));
   // launch the second thread
   t2.Start();
   // and meanwhile call the Countdown method from the first thread
   Countdown();
 }

}


      </source>


illustrates the use of thread-local storage

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example14_4.cs illustrates the use of thread-local storage
  • /

using System; using System.Threading; public class Example14_4 {

 // the WriteError method writes error info from the current thread
 public static void WriteError()
 {
   Console.WriteLine("Error number = " + Thread.GetData(Thread.GetNamedDataSlot("ErrNo")));
   Console.WriteLine("Error source = " + Thread.GetData(Thread.GetNamedDataSlot("ErrSource")));
 }
 // the SetError method sets a random error number
 public static void SetError() 
 {
   Random r = new Random();
   Thread.SetData(Thread.GetNamedDataSlot("ErrNo"), r.Next(100));
   Thread.SetData(Thread.GetNamedDataSlot("ErrSource") ,Thread.CurrentThread.Name);
   WriteError();
 }
 public static void Main() 
 {
   // allocate some named data slots
   Thread.AllocateNamedDataSlot("ErrNo");
   Thread.AllocateNamedDataSlot("ErrSource");
   // create and start a second thread
   Thread t2 = new Thread(new ThreadStart(SetError));
   t2.Name = "t2";
   t2.Start();
   // create a third thread
   Thread t3 = new Thread(new ThreadStart(SetError));
   t3.Name = "t3";
   t3.Start();
   // clean up the data slots
   Thread.FreeNamedDataSlot("ErrNo");
   Thread.FreeNamedDataSlot("ErrSource");
 }

}


      </source>


My Threading Start

<source lang="csharp"> /*

* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/

using System; using System.Threading; namespace Client.Chapter_15___Threading {

 public class MyThreadingClassChapter_15___Threading
 {
   static void Main(string[] args)
   {
     My2ndClass me = new My2ndClass();
     Thread[] MyThreads = new Thread[10];
     for (int I = 0; I < 100; I++)
     {
       MyThreads[I] = new Thread(new ThreadStart(me.MyThreadProc));
       MyThreads[I].Start();
     }
   }
 }
 class My2ndClass
 {
   private int counter;
   public void MyThreadProc()
   {
     IncCounter();
   }
   private void IncCounter()
   {
     lock (this)
     {
       counter++;
     }
   }
 }

}

      </source>


shows the Thread.Join method in action

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example14_6.cs shows the Thread.Join method in action
  • /

using System; using System.Threading; public class Example14_6 {

 // the Countdown method counts down from 1000 to 1
 public static void Countdown() 
 {
   for (int counter = 1000; counter > 0; counter--) 
   {
     Console.Write(counter.ToString() + " ");
   }
 }
 public static void Main() 
 {
   // create a second thread
   Thread t2 = new Thread(new ThreadStart(Countdown));
   // launch the second thread
   t2.Start();
   // block the first thread until the second is done
   t2.Join();
   // and  call the Countdown method from the first thread
   Countdown();
 }

}


      </source>


Suspending, resuming, and stopping a thread

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Suspending, resuming, and stopping a thread.

using System; using System.Threading;

class MyThread {

 public Thread thrd;  
   
 public MyThread(string name) {  
   thrd = new Thread(new ThreadStart(this.run)); 
   thrd.Name = name; 
   thrd.Start();  
 }  
 
 // This is the entry point for thread.  
 void run() {  
   Console.WriteLine(thrd.Name + " starting."); 

   for(int i = 1; i <= 1000; i++) {  
     Console.Write(i + " ");  
     if((i%10)==0) { 
       Console.WriteLine(); 
       Thread.Sleep(250); 
     } 
   } 
   Console.WriteLine(thrd.Name + " exiting.");  
 }  

}

public class SuspendResumeStop {

 public static void Main() {  
   MyThread mt1 = new MyThread("My Thread");  

   Thread.Sleep(1000); // let child thread start executing 
 
   mt1.thrd.Suspend();  
   Console.WriteLine("Suspending thread.");  
   Thread.Sleep(1000); 

   mt1.thrd.Resume();  
   Console.WriteLine("Resuming thread.");  
   Thread.Sleep(1000); 

   mt1.thrd.Suspend();  
   Console.WriteLine("Suspending thread.");  
   Thread.Sleep(1000); 

   mt1.thrd.Resume();  
   Console.WriteLine("Resuming thread.");  
   Thread.Sleep(1000); 

   Console.WriteLine("Stopping thread.");  
   mt1.thrd.Abort(); 

   mt1.thrd.Join(); // wait for thread to terminate 

   Console.WriteLine("Main thread terminating.");  
 }  

}


      </source>