Csharp/C Sharp/Thread/Thread Properties

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

Current Thread Properties

 
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
class Program {
    static int interval;
    static void Main(string[] args) {
        interval = 100;
        ThreadPool.QueueUserWorkItem(new WaitCallback(StartMethod));
        Thread.Sleep(100);
        ThreadPool.QueueUserWorkItem(new WaitCallback(StartMethod));
        Console.ReadLine();
    }
    static void StartMethod(Object stateInfo) {
        DisplayNumbers("Thread " + DateTime.Now.Millisecond.ToString());
        Console.WriteLine("Thread Finished");
    }
    static void DisplayNumbers(string GivenThreadName) {
        Console.WriteLine("Starting thread: " + GivenThreadName);
        for (int i = 1; i <= 8 * interval; i++) {
            if (i % interval == 0) {
                Console.WriteLine("Count has reached " + i);
                Console.WriteLine("CurrentCulture: " + Thread.CurrentThread.CurrentCulture.ToString());
                Console.WriteLine("IsThreadPoolThread: " + Thread.CurrentThread.IsThreadPoolThread.ToString());
                Console.WriteLine("ManagedThreadId: " + Thread.CurrentThread.ManagedThreadId.ToString());
                Console.WriteLine("Priority: " + Thread.CurrentThread.Priority.ToString());
                Console.WriteLine("ThreadState: " + Thread.CurrentThread.ThreadState.ToString());
                Thread.Sleep(1000);
            }
        }
    }
}


Demonstrate thread priorities

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Demonstrate thread priorities. 
 
using System; 
using System.Threading; 
 
class MyThread { 
  public int count; 
  public Thread thrd; 
 
  static bool stop = false; 
  static string currentName; 
 
  /* Construct a new thread. Notice that this  
     constructor does not actually start the 
     threads running. */ 
  public MyThread(string name) { 
    count = 0; 
    thrd = new Thread(new ThreadStart(this.run)); 
    thrd.Name = name; 
    currentName = name; 
  } 
 
  // Begin execution of new thread. 
  void run() { 
    Console.WriteLine(thrd.Name + " starting."); 
    do { 
      count++; 
 
      if(currentName != thrd.Name) { 
        currentName = thrd.Name; 
        Console.WriteLine("In " + currentName); 
      } 
 
    } while(stop == false && count < 1000000000); 
    stop = true; 
 
    Console.WriteLine(thrd.Name + " terminating."); 
  } 
} 
 
public class PriorityDemo { 
  public static void Main() { 
    MyThread mt1 = new MyThread("High Priority"); 
    MyThread mt2 = new MyThread("Low Priority"); 
 
    // Set the priorities. 
    mt1.thrd.Priority = ThreadPriority.AboveNormal; 
    mt2.thrd.Priority = ThreadPriority.BelowNormal; 
 
    // Start the threads. 
    mt1.thrd.Start(); 
    mt2.thrd.Start(); 
 
    mt1.thrd.Join(); 
    mt2.thrd.Join(); 
 
    Console.WriteLine(); 
    Console.WriteLine(mt1.thrd.Name + " thread counted to " + 
                      mt1.count); 
    Console.WriteLine(mt2.thrd.Name + " thread counted to " + 
                      mt2.count); 
  } 
}


illustrates the ThreadState property

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example14_3.cs illustrates the ThreadState property
*/
using System;
using System.Threading;
public class Example14_3 
{
  // the Countdown method counts down from 10 to 1
  public static void Countdown() 
  {
    for (int counter = 10; counter > 0; counter--) 
    {
      Console.Write(counter.ToString() + " ");
    }
    Console.WriteLine();
  }
  // the DumpThreadState method displays the current Thread"s state
  // Note that ThreadState is a bitmask, and multiple states for the
  // same thread are valid
  public static void DumpThreadState (
    Thread t
    ) 
  {
    Console.Write("Current state: ");
    if ((t.ThreadState & ThreadState.Aborted) == ThreadState.Aborted)
      Console.Write("Aborted ");
    if ((t.ThreadState & ThreadState.AbortRequested) == 
     ThreadState.AbortRequested)
      Console.Write("AbortRequested ");
    if ((t.ThreadState & ThreadState.Background) == 
     ThreadState.Background)
      Console.Write("Background ");
    if ((t.ThreadState & 
     (ThreadState.Stopped | ThreadState.Unstarted | 
     ThreadState.Aborted)) == 0)
      Console.Write("Running ");
    if ((t.ThreadState & ThreadState.Stopped) == ThreadState.Stopped)
      Console.Write("Stopped ");
    if ((t.ThreadState & ThreadState.StopRequested) == 
     ThreadState.StopRequested)
      Console.Write("StopRequested ");
    if ((t.ThreadState & ThreadState.Suspended) == 
     ThreadState.Suspended)
      Console.Write("Suspended ");
    if ((t.ThreadState & ThreadState.SuspendRequested) == 
     ThreadState.SuspendRequested)
      Console.Write("SuspendRequested ");
    if ((t.ThreadState & ThreadState.Unstarted) == 
     ThreadState.Unstarted)
      Console.Write("Unstarted ");
    if ((t.ThreadState & ThreadState.WaitSleepJoin) ==
     ThreadState.WaitSleepJoin)
      Console.Write("WaitSleepJoin ");
    Console.WriteLine();
  }
  public static void Main() 
  {
    // create a second thread
    Thread t2 = new Thread(new ThreadStart(Countdown));
    DumpThreadState(t2);
    // launch the second thread
    t2.Start();
    DumpThreadState(t2);
    // and meanwhile call the Countdown method from the first thread
    Countdown();
    // shut down the second thread
    t2.Abort();
    DumpThreadState(t2);
  }
}


illustrates the use of thread priorities

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example14_2.cs illustrates the use of thread priorities
*/
using System;
using System.Threading;
public class Example14_2 
{
  // 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));
    // set the new thread to highest priority
    t2.Priority=ThreadPriority.Highest;
    // Locate the current thread and set it to the lowest priority
    Thread.CurrentThread.Priority=ThreadPriority.Lowest;
    // launch the second thread
    t2.Start();
    // and meanwhile call the Countdown method from the first thread
    Countdown();
  }
}


Use IsAlive to wait for threads to end

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System;
// Use IsAlive to wait for threads to end. 
public class MoreThreads2 { 
  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.thrd.IsAlive && 
             mt2.thrd.IsAlive && 
             mt3.thrd.IsAlive); 
 
    Console.WriteLine("Main thread ending."); 
  } 
}