|
分類:[C#]
「ThreadPoolに溜まっている各スレッド全体の完了待ち」の方法を教えて下さい。
/*
* 環境 VS2008 FW3.5
*/
using System;
using System.Threading;
public class TaskInfo {
public string sBoilerplate;
public int iValue;
public TaskInfo( string sText_ , int oNumber_ ) {
sBoilerplate = sText_;
iValue = oNumber_;
}
}
public class Example {
public static void Main() {
const int iNumbOfTask = 100000;
TaskInfo[] oTasks = new TaskInfo[iNumbOfTask];
WaitCallback[] oWCall = new WaitCallback[iNumbOfTask];
int iWorkerThreads;
int iPortThreads;
for ( int i= 0 ; i < oTasks.Length ; i++ ) {
oTasks[i] = new TaskInfo("data = " , i);
oWCall[i] = new WaitCallback(ThreadProc);
}
ThreadPool.GetMaxThreads(out iWorkerThreads , out iPortThreads);
Console.WriteLine("A : " + iWorkerThreads.ToString() + " : " + iPortThreads.ToString());
ThreadPool.SetMaxThreads(4 , 4);
ThreadPool.GetMaxThreads(out iWorkerThreads , out iPortThreads);
Console.WriteLine("B : " + iWorkerThreads.ToString() + " : " + iPortThreads.ToString());
for ( int i= 0 ; i < oTasks.Length ; i++ ) {
for ( ; ; ) {
try {
ThreadPool.QueueUserWorkItem(oWCall[i] , oTasks[i]);
break;
} catch ( Exception e ) {
Thread.Sleep(1000);
}
}
}
/*
ThreadPool に 溜まっている 各スレッドの全体の完了待ち
の方法が書かれているサイトを教えて下さい。
*/
Thread.Sleep(10000);
ThreadPool.GetMaxThreads(out iWorkerThreads , out iPortThreads);
Console.WriteLine("C : " + iWorkerThreads.ToString() + " : " + iPortThreads.ToString());
Console.WriteLine("Main thread exits.");
}
static void ThreadProc( Object oStateInfo_ ) {
Thread.Sleep(1000);
TaskInfo oTInfo = (TaskInfo)oStateInfo_;
Console.WriteLine(oTInfo.sBoilerplate + oTInfo.iValue.ToString());
}
}
|