- I/O 集中型の操作を行っています。
- 一度に最大 5 つのスレッドを実行したいだけです。
- キューに入れて完了するタスクが 8000 個あります。
- 各タスクの実行には約 15 ~ 20 秒かかります。
ThreadPoolを見回しましたが、
ThreadPool.SetMaxThreads(5, 0);
List<task> tasks = GetTasks();
int toProcess = tasks.Count;
ManualResetEvent resetEvent = new ManualResetEvent(false);
for (int i = 0; i < tasks.Count; i++)
{
ReportGenerator worker = new ReportGenerator(tasks[i].Code, id);
ThreadPool.QueueUserWorkItem(x =>
{
worker.Go();
if (Interlocked.Decrement(ref toProcess) == 0)
resetEvent.Set();
});
}
resetEvent.WaitOne();
理由がわかりません... 私のコードは一度に 5 つ以上のスレッドを実行しています。maxthreads、setminthreads を設定しようとしましたが、5 つ以上のスレッドを実行し続けます。
何が起こっている?私は何が欠けていますか?これを別の方法で行う必要がありますか?
ありがとう