【发布时间】:2010-07-26 10:59:18
【问题描述】:
我有一个包含我要下载的项目的列表。我使用 for 循环来迭代列表。
对于此列表中的每个项目,我都会启动一个引用该项目的新线程。我的问题是我想同时限制 maxDownload。
for (int i = downloadList.Count - 1; i >= 0; i--)
{
downloadItem item = downloadList[i];
if (item.Status != 1 && item.Status != 2)
{
ThreadStart starter = delegate { this.DownloadItem(ref item); };
Thread t = new Thread(starter);
t.IsBackground = true;
t.Name = item.Name;
t.Priority = ThreadPriority.Normal;
t.Start();
}
}
我阅读了一些关于 ThreadPool 的内容,但是我无法引用我的项目。有人能帮我吗?谢谢! :)
编辑:
我对此进行了测试:
ThreadPool.SetMaxThreads(maxDownloads, maxDownloads);
ThreadPool.SetMinThreads(maxDownloads, maxDownloads);
ThreadPool.QueueUserWorkItem(DownloadItem, ref item);
我不知道如何用这个线程引用我的 downloadItem .....
【问题讨论】:
-
由线程池服务的工作队列听起来是正确的方法 - 您能否更具体地说明您尝试使用它时遇到的问题?
-
查看线程池是如何使用的:msdn.microsoft.com/en-us/library/3dasc8as(VS.80).aspx您没有在代码中使用它们。
-
你几乎肯定会更好地研究如何使用线程池而不是自己创建一大堆短期线程
标签: c# multithreading limit threadpool