Csharp/C Sharp/Thread/Thread Pool
Содержание
illustrates the use of the system thread pool
<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110
- /
/*
Example14_12.cs illustrates the use of the system thread pool
- /
using System; using System.Threading; public class Example14_12 {
// the Countdown method counts down from 1000 to 1 public static void Countdown(Object o) { for (int counter = 1000; counter > 0; counter--) { Console.Write(counter.ToString() + " "); } } public static void Main() { // ask the system to create and launch a second thread ThreadPool.QueueUserWorkItem(new WaitCallback(Countdown), null); // and meanwhile call the Countdown method from the first thread Countdown(null); }
}
</source>
Thread pool demo
<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 MyMainClassChapter_15___Threading { public static ManualResetEvent MyManualEvent = new ManualResetEvent(false); public static AutoResetEvent MyAutoEvent = new AutoResetEvent(false); static void Main(string[] args) { ThreadPool.QueueUserWorkItem(new WaitCallback(DoBackgroundWorkManual)); MyManualEvent.WaitOne(); MyManualEvent.Reset(); //Sets the Event back to nonsignaled ThreadPool.QueueUserWorkItem(new WaitCallback(DoBackgroundWorkAuto)); MyAutoEvent.WaitOne(); } public static void DoBackgroundWorkManual(Object state) { Console.WriteLine("Thread 1"); //Do stuff //Then release control back to the main thread MyManualEvent.Set(); //Signals the event } public static void DoBackgroundWorkAuto(Object state) { Console.WriteLine("Thread 1"); //Do stuff MyAutoEvent.Set(); } }
}
</source>
ThreadPool.QueueUserWorkItem
<source lang="csharp">
using System; using System.Threading;
class WinterLocked {
public ManualResetEvent a = new ManualResetEvent(false); private int i = 5; public void Run(object s) { Interlocked.Increment(ref i); Console.WriteLine("{0} {1}", Thread.CurrentThread.GetHashCode(), i); }
} public class MainApp {
public static void Main() { ManualResetEvent mR = new ManualResetEvent(false); WinterLocked wL = new WinterLocked(); for (int i = 1; i <= 10; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(wL.Run), 1); } mR.WaitOne(10000, true); }
}
</source>
ThreadPool.RegisterWaitForSingleObject
<source lang="csharp"> using System; using System.Threading; class MainClass {
private static void EventHandler(object state, bool timedout) { if (timedout) { Console.WriteLine("{0} : Wait timed out.", DateTime.Now.ToString("HH:mm:ss.ffff")); } else { Console.WriteLine("{0} : {1}", DateTime.Now.ToString("HH:mm:ss.ffff"), state); } } public static void Main() { AutoResetEvent autoEvent = new AutoResetEvent(false); string state = "AutoResetEvent signaled."; RegisteredWaitHandle handle = ThreadPool.RegisterWaitForSingleObject(autoEvent, EventHandler, state, 3000, false); while (Console.ReadLine().ToUpper() != "CANCEL") { autoEvent.Set(); } Console.WriteLine("Unregistering wait operation."); handle.Unregister(null); }
}
</source>
Thread Pool Sample
<source lang="csharp"> - using System; using System.Threading; public class ThreadPoolSample {
public static void Main() { ThreadPoolSample tps = new ThreadPoolSample(); } public ThreadPoolSample() { int i; ThreadPool.QueueUserWorkItem(new WaitCallback(Counter)); ThreadPool.QueueUserWorkItem(new WaitCallback(Counter2)); for(i = 0; i < 10; i++) { Console.WriteLine("main: {0}", i); Thread.Sleep(1000); } } void Counter(object state) { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread: {0}", i); Thread.Sleep(2000); } } void Counter2(object state) { int i; for (i = 0; i < 10; i++) { Console.WriteLine(" thread2: {0}", i); Thread.Sleep(3000); } }
}
</source>
Thread Pool Tcp Server
<source lang="csharp"> /* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765
- /
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; public class ThreadPoolTcpSrvr {
private TcpListener client; public ThreadPoolTcpSrvr() { client = new TcpListener(9050); client.Start(); Console.WriteLine("Waiting for clients..."); while(true) { while (!client.Pending()) { Thread.Sleep(1000); } ConnectionThread newconnection = new ConnectionThread(); newconnection.threadListener = this.client; ThreadPool.QueueUserWorkItem(new WaitCallback(newconnection.HandleConnection)); } } public static void Main() { ThreadPoolTcpSrvr tpts = new ThreadPoolTcpSrvr(); }
} class ConnectionThread {
public TcpListener threadListener; private static int connections = 0; public void HandleConnection(object state) { int recv; byte[] data = new byte[1024]; TcpClient client = threadListener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); connections++; Console.WriteLine("New client accepted: {0} active connections", connections); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); ns.Write(data, 0, data.Length); while(true) { data = new byte[1024]; recv = ns.Read(data, 0, data.Length); if (recv == 0) break; ns.Write(data, 0, recv); } ns.Close(); client.Close(); connections--; Console.WriteLine("Client disconnected: {0} active connections", connections); }
}
</source>