Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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# 同时调用多个DownloadFileAsync阻止UI线程_C#_Wpf_Multithreading_Task_Webclient - Fatal编程技术网

C# 同时调用多个DownloadFileAsync阻止UI线程

C# 同时调用多个DownloadFileAsync阻止UI线程,c#,wpf,multithreading,task,webclient,C#,Wpf,Multithreading,Task,Webclient,我正在制作一个WPF应用程序,使用WebClient从Web服务器下载文件。我的代码现在一次下载一个文件,并等待该文件完成后再开始下一个文件,以此类推。 我有几个存储文件名的列表,就像文件夹一样。当我单击第一个按钮时,下载开始下载第一个文件夹中的文件,但当第一个下载正在进行,我想通过单击第二个按钮开始从第二个文件夹下载时,我的应用程序冻结。我希望我的应用程序同时运行多个DownloadFileAsync。 我已经尝试过为每个donwloading启动一个新线程,但这似乎也不起作用 public

我正在制作一个WPF应用程序,使用WebClient从Web服务器下载文件。我的代码现在一次下载一个文件,并等待该文件完成后再开始下一个文件,以此类推。 我有几个存储文件名的列表,就像文件夹一样。当我单击第一个按钮时,下载开始下载第一个文件夹中的文件,但当第一个下载正在进行,我想通过单击第二个按钮开始从第二个文件夹下载时,我的应用程序冻结。我希望我的应用程序同时运行多个DownloadFileAsync。 我已经尝试过为每个donwloading启动一个新线程,但这似乎也不起作用

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    }

    private void StartButton_Click(object sender, RoutedEventArgs e)
    {


        DownloadGameFile dlg = new DownloadGameFile();
        dlg.StartDownload(11825);

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        DownloadGameFile dlg = new DownloadGameFile();
        dlg.StartDownload(11198);
    }
}

类下载游戏文件
{
私人下载的Torrent文件DLTorrent;
//已存在的文件列表
私有列表ExistFile=新列表();
目录信息文件信息;
私有字符串savePath=@“C:somewhere”;
公共字符串路径{get;set;}
私有bool isDelete=false;
公开下载游戏文件()
{
DLTorrent=新下载的TorrentFile();
}
公共异步void StartDownload(int torrentId)
{
尝试
{
DLTorrent.DecodeTorrent(torrentId);
//为游戏创建一个文件夹
var newdic=Directory.CreateDirectory(savePath+torrentId);
fileInfo=newdirectoryinfo(savePath+torrentId);
//目录中的文件信息
FileInfo[]files=FileInfo.GetFiles();
foreach(文件中的文件信息i)
{
Console.WriteLine(“文件退出”+i);
if(DLTorrent.gameinformation[i.Name]!=i.Length)
{
i、 删除();
isDelete=真;
}
其他的
{
Console.WriteLine(“添加文件”);
ExistFile.Add(i.Name);
}
}
//列出尚未下载的文件
var res=DLTorrent.gameinformation.Keys.Except(ExistFile);
var nFiles=files.Length;
如果(n文件==0 | |!isDelete)
{
nFiles++;
foreach(以res表示的var x)
{
((MainWindow)System.Windows.Application.Current.MainWindow).label1.Content=nFiles+“out of”+DLTorrent.gameinformation.Keys.Count();
等待下载协议(“http://cdn.somewhere/rental/“+torrentId+”/“+x,存储路径+torrentId+”/“+x);
nFiles++;
}
}
其他的
{
foreach(以res表示的var x)
{
((MainWindow)System.Windows.Application.Current.MainWindow).label1.Content=nFiles+“out of”+DLTorrent.gameinformation.Keys.Count();
等待下载协议(“http://cdn.somewhere/rental/“+torrentId+”/“+x,存储路径+torrentId+”/“+x);
nFiles++;
}
}
}
抓住
{
}
}
公共异步任务下载协议(字符串地址、字符串位置)
{
Uri=新的Uri(地址);
使用(WebClient=newWebClient())
{
client.DownloadProgressChanged+=(o,e)=>
{
Console.WriteLine(e.BytesReceived+“”+e.ProgressPercentage);
((MainWindow)System.Windows.Application.Current.MainWindow).DownloadBar.Value=e.ProgressPercentage;
};
client.DownloadFileCompleted+=(o,e)=>
{
如果(e.Cancelled==true)
{
Console.WriteLine(“下载已被取消”);
}
其他的
{
Console.WriteLine(“下载完成!”);
}
};
wait client.DownloadFileTaskAsync(Uri,位置);
}
}
}
您是否尝试过:

private async void StartButton_Click(object sender, RoutedEventArgs e)
{
    DownloadGameFile dlg = new DownloadGameFile();
    await dlg.StartDownload(11825);
}

dlg.StartDownload
必须返回任务。

。我已经用可以编译的正确代码更改了我的问题。您不能将Wait与webClient.DownloadFileAsync()一起使用。请参阅@alexm我在任何地方都看不到
下载文件async
?你是什么意思?谢谢你的回复@alexm,但我试图复制你的WebClientTextensions类并更改为Wait client.DownloadFileWaitiableAsync(Uri,location,CancellationToken.None);。但在我刚开始点击StartDownload按钮之后,当我点击button1时,它仍然会冻结我的应用程序。它冻结了一段时间,然后解冻并继续下载,但StartDownload_按钮的调用仍然在后台下载。这是@Eser所说的。我正在使用DownloadFileTaskAsync。
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
    DownloadGameFile dlg = new DownloadGameFile();
    await dlg.StartDownload(11825);
}