Csharp/C Sharp/Thread/Thread Monitor Manage — различия между версиями

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

Версия 15:31, 26 мая 2010

Control the main thread

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

// Control the main thread. 
 
using System; 
using System.Threading; 
 
public class UseMain { 
  public static void Main() { 
    Thread thrd; 
 
    // Get the main thread. 
    thrd = Thread.CurrentThread; 
 
    // Display main thread"s name. 
    if(thrd.Name == null) 
      Console.WriteLine("Main thread has no name."); 
    else 
      Console.WriteLine("Main thread is called: " + 
                         thrd.Name); 
 
    // Display main thread"s priority. 
    Console.WriteLine("Priority: " + 
                       thrd.Priority); 
 
    Console.WriteLine(); 
 
    // Set the name and priority. 
    Console.WriteLine("Setting name and priority.\n"); 
    thrd.Name = "Main Thread"; 
    thrd.Priority = ThreadPriority.AboveNormal; 
 
    Console.WriteLine("Main thread is now called: " + 
                       thrd.Name); 
 
    Console.WriteLine("Priority is now: " + 
                       thrd.Priority); 
  } 
}


illustrates the use of the Monitor object

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example14_10.cs illustrates the use of the Monitor object
*/
using System;
using System.Threading;
public class Example14_10 
{
  // a shared counter
  private int Runs = 0;
  // the CountUp method increments the shared counter
  public  void CountUp() 
  {
    while (Runs < 10)
    {
      Monitor.Enter(this);
      int Temp = Runs;
      Temp++;
      Console.WriteLine(Thread.CurrentThread.Name + " " + Temp);
      Thread.Sleep(1000);
      Runs = Temp;
      Monitor.Exit(this);
    } 
  }
  public static void Main() 
  {
    // Make an instance of this class
    Example14_10 ex = new Example14_10();
    // And run the test outside of the static method
    ex.RunThreads();
  }
  public void RunThreads()
  {
    // create and launch two threads
    Thread t2 = new Thread(new ThreadStart(CountUp));
    t2.Name = "t2";
    Thread t3 = new Thread(new ThreadStart(CountUp));
    t3.Name = "t3";
    t2.Start();
    t3.Start();
  }
}


Managing Threads

/*
 * 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 ManagingThreads
  {
    public AutoResetEvent A = new AutoResetEvent(false);
    public AutoResetEvent[] B = new AutoResetEvent[3];
    public static int Main()
    {
      ManagingThreads M = new ManagingThreads();
      M.InitEvents();
      Thread T = new Thread(new ThreadStart(M.MyNewThread));
      T.Start();
      //T.Join();  //Blocks this thread until T Stops
      Console.WriteLine("Waiting!");
      M.A.WaitOne();
      WaitHandle.WaitAll(M.B);
      return 0;
    }
    public void InitEvents()
    {
      for (int i = 0; i < 3; i++)
      {
        B[i] = new AutoResetEvent(false);
      }
    }
    public void MyNewThread()
    {
      Console.WriteLine("MyNewThread Called");
      A.Set();
      B[0].Set();
      B[1].Set();
      B[2].Set();
    }
  }
}


Threading Class Monitor

/*
 * 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.Collections;
using System.Threading;
namespace Client.Chapter_15___Threading
{
  public class ThreadingClassMonitor
  {
    public static Thread ThreadOne = ThreadOne = new Thread(new ThreadStart(MonitorExample));
    public static ArrayList MyList = new ArrayList();
    public ThreadingClassMonitor()
    {
      MyList.Add("Test1");
      MyList.Add("Test2");
    }
    static void Main(string[] args)
    {
      ThreadOne.Start();
    }
    protected static void MonitorExample()
    {
      Monitor.Enter(MyList);
      MyList.Add("Test3");
      Monitor.Exit(MyList);
    }
  }
}