c#的執行緒池,第一次用
阿新 • • 發佈:2019-02-15
static void ThreadFileDeal(object i)
{
string strGet = (string)i;
for (int a = 0; a < 5; a++)
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + strGet);
Thread.Sleep(1000);
}
}
static void WaitCallBackMethod(object param)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(String.Format("Thread {0} is running", param));
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
int ncount = 0;
while(ncount<100)
{
ncount++;
ThreadPool.SetMaxThreads(20, 20);
ThreadPool.QueueUserWorkItem(WaitCallBackMethod, ncount);//第一個啟動的方法
ThreadPool.QueueUserWorkItem(ThreadFileDeal,"temp");//第二個啟動的方法
}
Console.Read();
}
{
string strGet = (string)i;
for (int a = 0; a < 5; a++)
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + strGet);
Thread.Sleep(1000);
}
}
static void WaitCallBackMethod(object param)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(String.Format("Thread {0} is running", param));
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
int ncount = 0;
while(ncount<100)
{
ncount++;
ThreadPool.SetMaxThreads(20, 20);
ThreadPool.QueueUserWorkItem(WaitCallBackMethod, ncount);//第一個啟動的方法
ThreadPool.QueueUserWorkItem(ThreadFileDeal,"temp");//第二個啟動的方法
}
Console.Read();
}