using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Windows.Forms; using System.Diagnostics; namespace ThreadPool01 { class Program { static void Main(string[] args) { Program p = new Program(); Console.WriteLine("testing thread pool numbers:"); p.CallFoo30AsyncTimes(); Console.ReadKey(); } private void CallFoo30AsyncTimes() { // create a delegate of MethodInvoker // poiting to our Foo function. MethodInvoker simpleDelegate = new MethodInvoker(Foo); // Calling Foo Async 30 times. for (int i = 0; i < 30; i++) { // call Foo() simpleDelegate.BeginInvoke(null, null); } } private void Foo() { int intAvailableThreads, intAvailableIoAsynThreds; // ask the number of avaialbe threads on the pool, //we really only care about the first parameter. ThreadPool.GetAvailableThreads(out intAvailableThreads, out intAvailableIoAsynThreds); // build a message to log string strMessage = String.Format(@"Is Thread Pool: {0}, Thread Id: {1} Free Threads {2}", Thread.CurrentThread.IsThreadPoolThread.ToString(), Thread.CurrentThread.GetHashCode(), intAvailableThreads); // check if the thread is on the thread pool. Trace.WriteLine(strMessage); // create a delay... Thread.Sleep(3000); return; } } }
Console.WriteLine(strMessage);
Trace.WriteLine(strMessage);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)