c#多執行緒,原理和常用方法
阿新 • • 發佈:2019-02-06
using System;
using System.Text;
using System.Threading;
namespace 多執行緒
{
publicclass Example
{
publicstaticvoid Main()
{
// Queue the task. ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
Console.WriteLine("Main thread does some work, then sleeps. ");
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
}
staticvoid ThreadProc(Object stateInfo)
{
// No state object was passed to QueueUserWorkItem,
// so stateInfo is null. Console.WriteLine("Hello from the thread pool. ");
}
}
}
using System.Text;
using System.Threading;
namespace 多執行緒
{
publicclass Example
{
publicstaticvoid Main()
{
// Queue the task. ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
Console.WriteLine("Main thread does some work, then sleeps.
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
}
staticvoid ThreadProc(Object stateInfo)
{
// No state object was passed to QueueUserWorkItem,
// so stateInfo is null. Console.WriteLine("Hello from the thread pool.
}
}
}