Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不同线程调用函数时出现的问题_C#_Multithreading_Windows Phone 7_Asynchronous_Webclient - Fatal编程技术网

C# 不同线程调用函数时出现的问题

C# 不同线程调用函数时出现的问题,c#,multithreading,windows-phone-7,asynchronous,webclient,C#,Multithreading,Windows Phone 7,Asynchronous,Webclient,我有一个功能。我想通过不同的线程多次调用这个函数。我该怎么做?。我的功能如下 public void DownloadImage(List<String> imageUrl) { imageCount = imageUrl.Count; foreach (string url in imageUrl) { StartDownload(url); } } public void

我有一个功能。我想通过不同的线程多次调用这个函数。我该怎么做?。我的功能如下

public void DownloadImage(List<String> imageUrl)
    {
        imageCount = imageUrl.Count;

        foreach (string url in imageUrl)
        {
            StartDownload(url);
        }
    }
public void DownloadImage(列表imageUrl)
{
imageCount=imageUrl.Count;
foreach(imageUrl中的字符串url)
{
开始下载(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();
        }
参数化线程启动启动器;
对于(int i=0;i<10;i++)
{
_imageDownloader=new imageDownloader();//这是我在ie DownloadImage上面定义函数的类
_imageDownloader.OnCompleted+=新完成(\u imageDownloader\u OnCompleted);
starter=new ParameterizedThreadStart(_imageDownloader.DownloadImage);//在这一行中,它显示一个编译错误“DownloadImage的无重载与委托“System.Threading.ParameterizedThreadStart”匹配”
螺纹图像螺纹=新螺纹(起动器);
imageThread.Start();
}

请提供帮助。

您可以使用任务并行库:

public void DownloadImage(List<String> imageUrl)
{
    Parallel.ForEach(imageUrl, url => StartDownload(url));
}
public void DownloadImage(列表imageUrl)
{
Parallel.ForEach(imageUrl,url=>StartDownload(url));
}