我有一个功能。我想通过不同的线程多次调用此函数。我怎样才能做到这一点 ?。我的功能在
之下public void DownloadImage(List<String> imageUrl)
{
imageCount = imageUrl.Count;
foreach (string url in imageUrl)
{
StartDownload(url);
}
}
我要下载10张图片。我正在使用webclient下载图像。所以我想用10个线程来调用这个函数。我怎么能这样做?
我尝试了以下代码。但它显示编译错误
ParameterizedThreadStart starter;
for (int i = 0; i < 10; i++)
{
_imageDownloader = new ImageDownloader(); //this is class where I defined the function above ie DownloadImage
_imageDownloader.OnCompleted+=new Completed(_imageDownloader_OnCompleted);
starter = new ParameterizedThreadStart(_imageDownloader.DownloadImage); // in this line it showing a compile error "No overload for 'DownloadImage' matches delegate 'System.Threading.ParameterizedThreadStart'"
Thread imageThread = new Thread(starter);
imageThread.Start();
}
请帮忙。
答案 0 :(得分:2)
您可以使用任务并行库:
public void DownloadImage(List<String> imageUrl)
{
Parallel.ForEach(imageUrl, url => StartDownload(url));
}