<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FThread%2FThreadPool</id>
		<title>Csharp/CSharp Tutorial/Thread/ThreadPool - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FThread%2FThreadPool"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/ThreadPool&amp;action=history"/>
		<updated>2026-04-29T17:23:09Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/ThreadPool&amp;diff=6662&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/ThreadPool&amp;diff=6662&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/ThreadPool&amp;diff=6663&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/ThreadPool&amp;diff=6663&amp;oldid=prev"/>
				<updated>2010-05-26T12:20:12Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Available worker/IO threads==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Security;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        int w, io;&lt;br /&gt;
        ThreadPool.GetAvailableThreads(out w, out io);&lt;br /&gt;
        Console.WriteLine(w);&lt;br /&gt;
        Console.WriteLine(io);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;25&lt;br /&gt;
1000&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Max worker/IO threads in a ThreadPool==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Security;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        int maxW, maxIO;&lt;br /&gt;
        ThreadPool.GetMaxThreads(out maxW, out maxIO);&lt;br /&gt;
        Console.WriteLine(maxW);&lt;br /&gt;
        Console.WriteLine(maxIO);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;25&lt;br /&gt;
1000&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Min worker/IO threads in a ThreadPool==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Security;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        int minW, minIO;&lt;br /&gt;
        ThreadPool.GetMinThreads(out minW, out minIO);&lt;br /&gt;
        Console.WriteLine(minW);&lt;br /&gt;
        Console.WriteLine(minIO);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1&lt;br /&gt;
1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Registering wait callbacks for events==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Security;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        using (EventWaitHandle ewh = new ManualResetEvent(false))&lt;br /&gt;
        using (EventWaitHandle callbackDoneEvent = new ManualResetEvent(false))&lt;br /&gt;
        {&lt;br /&gt;
            ThreadPool.RegisterWaitForSingleObject(ewh,&lt;br /&gt;
                delegate {&lt;br /&gt;
                    Console.WriteLine(&amp;quot;Callback fired: {0}&amp;quot;, Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;
                    callbackDoneEvent.Set();&lt;br /&gt;
                }, null, Timeout.Infinite, true);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Setting the event: {0}&amp;quot;, Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;
            ewh.Set();&lt;br /&gt;
            callbackDoneEvent.WaitOne();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Setting the event: 1&lt;br /&gt;
Callback fired: 4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Scheduling work to occur on the thread-pool==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime;&lt;br /&gt;
using System.Runtime.rupilerServices;&lt;br /&gt;
using System.Security;&lt;br /&gt;
using System.Text;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        using (ManualResetEvent mre = new ManualResetEvent(false))&lt;br /&gt;
        {&lt;br /&gt;
            ThreadPool.QueueUserWorkItem(new WaitCallback(MyThreadPoolWorker), mre);&lt;br /&gt;
            &lt;br /&gt;
            Console.WriteLine(&amp;quot;Continuing work on the main thread: {0}&amp;quot;,&lt;br /&gt;
                Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;
            mre.WaitOne();&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
    }&lt;br /&gt;
    private static void MyThreadPoolWorker(object state)&lt;br /&gt;
    {&lt;br /&gt;
        ManualResetEvent mre = (ManualResetEvent)state;&lt;br /&gt;
        Console.WriteLine(&amp;quot;Work occurring on the thread-pool: {0}&amp;quot;,&lt;br /&gt;
            Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;
        // set the event to let our caller know we&amp;quot;re done:&lt;br /&gt;
        mre.Set();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Work occurring on the thread-pool: 3&lt;br /&gt;
Continuing work on the main thread: 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==ThreadPool Demo==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
  public class Printer&lt;br /&gt;
  {&lt;br /&gt;
    private object lockToken = new object();&lt;br /&gt;
    public void PrintNumbers()&lt;br /&gt;
    {&lt;br /&gt;
      lock (lockToken)&lt;br /&gt;
      {&lt;br /&gt;
        Console.WriteLine(&amp;quot;-&amp;gt; {0} is executing PrintNumbers()&amp;quot;,Thread.CurrentThread.Name);&lt;br /&gt;
        for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
        {&lt;br /&gt;
          Console.Write(&amp;quot;{0}, &amp;quot;, i);&lt;br /&gt;
          Thread.Sleep(1000);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  class Program&lt;br /&gt;
  {&lt;br /&gt;
    static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Main thread started. ThreadID = {0}&amp;quot;,Thread.CurrentThread.ManagedThreadId); &lt;br /&gt;
      Printer p = new Printer();&lt;br /&gt;
      WaitCallback workItem = new WaitCallback(PrintTheNumbers);&lt;br /&gt;
      for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
      {&lt;br /&gt;
        ThreadPool.QueueUserWorkItem(workItem, p);&lt;br /&gt;
      }&lt;br /&gt;
      Console.ReadLine();&lt;br /&gt;
    }&lt;br /&gt;
    static void PrintTheNumbers(object state)&lt;br /&gt;
    {&lt;br /&gt;
      Printer task = (Printer)state;&lt;br /&gt;
      task.PrintNumbers();&lt;br /&gt;
    }&lt;br /&gt;
  }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Thread Pool Example==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
   public static void Main()&lt;br /&gt;
   {&lt;br /&gt;
      ThreadPool.QueueUserWorkItem(new WaitCallback(Counter));&lt;br /&gt;
      ThreadPool.QueueUserWorkItem(new WaitCallback(Counter2));&lt;br /&gt;
      for(int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
      {&lt;br /&gt;
         Console.WriteLine(&amp;quot;main: {0}&amp;quot;, i);&lt;br /&gt;
         Thread.Sleep(1000);&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
   static void Counter(object state)&lt;br /&gt;
   {&lt;br /&gt;
      for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
      {&lt;br /&gt;
         Console.WriteLine(&amp;quot;  thread: {0}&amp;quot;, i);&lt;br /&gt;
         Thread.Sleep(2000);&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
   static void Counter2(object state)&lt;br /&gt;
   {&lt;br /&gt;
      for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
      {&lt;br /&gt;
         Console.WriteLine(&amp;quot;    thread2: {0}&amp;quot;, i);&lt;br /&gt;
         Thread.Sleep(3000);&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use the system thread pool==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Countdown(Object o) &lt;br /&gt;
  {&lt;br /&gt;
    for (int i = 1000; i &amp;gt; 0; i--) &lt;br /&gt;
    {&lt;br /&gt;
      Console.Write(i.ToString() + &amp;quot; &amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void Main() &lt;br /&gt;
  {&lt;br /&gt;
    ThreadPool.QueueUserWorkItem(new WaitCallback(Countdown), null);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1000 999 998 997 996 995 994 993 992 991 990 989 988&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use ThreadPool==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static ManualResetEvent MyManualEvent = new ManualResetEvent(false);&lt;br /&gt;
  public static AutoResetEvent MyAutoEvent = new AutoResetEvent(false);&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    ThreadPool.QueueUserWorkItem(new WaitCallback(DoBackgroundWorkManual));&lt;br /&gt;
    MyManualEvent.WaitOne();&lt;br /&gt;
    MyManualEvent.Reset();&lt;br /&gt;
    ThreadPool.QueueUserWorkItem(new WaitCallback(DoBackgroundWorkAuto));&lt;br /&gt;
    MyAutoEvent.WaitOne();&lt;br /&gt;
  }&lt;br /&gt;
  public static void DoBackgroundWorkManual(Object state)&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;Thread 1&amp;quot;);&lt;br /&gt;
    MyManualEvent.Set();&lt;br /&gt;
  }&lt;br /&gt;
  public static void DoBackgroundWorkAuto(Object state)&lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;Thread 1&amp;quot;);&lt;br /&gt;
    MyAutoEvent.Set();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Thread 1&lt;br /&gt;
Thread 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use ThreadPool to implement a hello server==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Net;&lt;br /&gt;
using System.Net.Sockets;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void serve( Object obj )&lt;br /&gt;
  {&lt;br /&gt;
    using ( Socket s = (Socket)obj )&lt;br /&gt;
    {&lt;br /&gt;
      Encoding enc = Encoding.GetEncoding( &amp;quot;ASCII&amp;quot; );&lt;br /&gt;
      Byte[] buff = enc.GetBytes( &amp;quot;hello&amp;quot; );&lt;br /&gt;
      s.Send( buff );&lt;br /&gt;
      s.Shutdown( SocketShutdown.Both );&lt;br /&gt;
      s.Close();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    using ( Socket svr = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ) )&lt;br /&gt;
    {&lt;br /&gt;
      svr.Bind( new IPEndPoint( IPAddress.Loopback, 8888 ) );&lt;br /&gt;
      svr.Listen( 5 );&lt;br /&gt;
      while ( true ) {&lt;br /&gt;
        Socket req = svr.Accept();&lt;br /&gt;
        ThreadPool.QueueUserWorkItem( new WaitCallback( serve ), req );&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using the thread pool and no arguments==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void DisplayMessage(object state)&lt;br /&gt;
    {&lt;br /&gt;
        string config = state as string;&lt;br /&gt;
        Console.WriteLine(config);&lt;br /&gt;
        Thread.Sleep(1000);&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        ThreadPool.QueueUserWorkItem(DisplayMessage);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;info&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using the thread pool and providing an argument==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void DisplayMessage(object state)&lt;br /&gt;
    {&lt;br /&gt;
        string config = state as string;&lt;br /&gt;
        Console.WriteLine(config);&lt;br /&gt;
        Thread.Sleep(1000);&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        ThreadPool.QueueUserWorkItem(DisplayMessage, &amp;quot;info&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using ThreadPool Instead of Instantiating Threads Explicitly==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class ThreadPools&lt;br /&gt;
{&lt;br /&gt;
  public const int Repetitions = 1000;&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
      ThreadPool.QueueUserWorkItem(DoWork, &amp;quot;.&amp;quot;);&lt;br /&gt;
      for (int count = 0; count &amp;lt; Repetitions; count++)&lt;br /&gt;
      {&lt;br /&gt;
          Console.Write(&amp;quot;-&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      Thread.Sleep(1000);&lt;br /&gt;
  }&lt;br /&gt;
  public static void DoWork(object state)&lt;br /&gt;
  {&lt;br /&gt;
      for (int count = 0; count &amp;lt; Repetitions; count++)&lt;br /&gt;
      {&lt;br /&gt;
          Console.Write(state);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>