同时调用多个 DownloadFileAsync 阻塞 UI 个线程
Calling multiple DownloadFileAsync at the same time block UI thread
我正在制作一个 WPF 应用程序,我在其中使用 WebClient 从网络服务器下载文件。我的代码现在一次下载一个文件并等待该文件完成,然后再开始下一个文件,依此类推。
我有几个存储文件名称的列表 - 就像一个文件夹。当我单击第一个按钮时,下载开始依次下载第一个文件夹中的文件,但是当第一次下载正在进行时,我想通过单击第二个按钮从第二个文件夹开始下载,我的应用程序冻结了。我希望我的应用程序同时 运行 多个 DownloadFileAsync。
我试图为每次下载启动一个新线程,但这似乎也不起作用。
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);
}
}
class DownloadGameFile
{
private DownloadTorrentFile DLTorrent;
//List of file that already exist
private List<string> ExistFile = new List<string>();
DirectoryInfo fileInfo;
private string savePath = @"C:somewhere";
public string path { get; set; }
private bool isDelete = false;
public DownloadGameFile()
{
DLTorrent = new DownloadTorrentFile();
}
public async void StartDownload(int torrentId)
{
try
{
DLTorrent.DecodeTorrent(torrentId);
//Creates a folder to the game
var newdic = Directory.CreateDirectory(savePath + torrentId);
fileInfo = new DirectoryInfo(savePath + torrentId);
//File info from a Directory
FileInfo[] files = fileInfo.GetFiles();
foreach (FileInfo i in files)
{
Console.WriteLine("Files exit " + i);
if (DLTorrent.GameInfomation[i.Name] != i.Length)
{
i.Delete();
isDelete = true;
}
else
{
Console.WriteLine("add files ");
ExistFile.Add(i.Name);
}
}
//Make a list which file not downloaded yet
var res = DLTorrent.GameInfomation.Keys.Except(ExistFile);
var nFiles = files.Length;
if(nFiles == 0 || !isDelete)
{
nFiles++;
foreach (var x in res)
{
((MainWindow)System.Windows.Application.Current.MainWindow).label1.Content = nFiles + " out of " + DLTorrent.GameInfomation.Keys.Count();
await DownloadProtocol("http://cdn.somewhere/rental/" + torrentId + "/" + x, savePath + torrentId + "/" + x);
nFiles++;
}
}
else
{
foreach (var x in res)
{
((MainWindow)System.Windows.Application.Current.MainWindow).label1.Content = nFiles + " out of " + DLTorrent.GameInfomation.Keys.Count();
await DownloadProtocol("http://cdn.somewhere/rental/" + torrentId + "/" + x, savePath + torrentId + "/" + x);
nFiles++;
}
}
}
catch
{
}
}
public async Task DownloadProtocol(string address, string location)
{
Uri Uri = new Uri(address);
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += (o, e) =>
{
Console.WriteLine(e.BytesReceived + " " + e.ProgressPercentage);
((MainWindow)System.Windows.Application.Current.MainWindow).DownloadBar.Value = e.ProgressPercentage;
};
client.DownloadFileCompleted += (o, e) =>
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
};
await client.DownloadFileTaskAsync(Uri, location);
}
}
}
你试过这个吗:
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
DownloadGameFile dlg = new DownloadGameFile();
await dlg.StartDownload(11825);
}
dlg.StartDownload
必须 return 一个任务。
我正在制作一个 WPF 应用程序,我在其中使用 WebClient 从网络服务器下载文件。我的代码现在一次下载一个文件并等待该文件完成,然后再开始下一个文件,依此类推。 我有几个存储文件名称的列表 - 就像一个文件夹。当我单击第一个按钮时,下载开始依次下载第一个文件夹中的文件,但是当第一次下载正在进行时,我想通过单击第二个按钮从第二个文件夹开始下载,我的应用程序冻结了。我希望我的应用程序同时 运行 多个 DownloadFileAsync。 我试图为每次下载启动一个新线程,但这似乎也不起作用。
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);
}
}
class DownloadGameFile
{
private DownloadTorrentFile DLTorrent;
//List of file that already exist
private List<string> ExistFile = new List<string>();
DirectoryInfo fileInfo;
private string savePath = @"C:somewhere";
public string path { get; set; }
private bool isDelete = false;
public DownloadGameFile()
{
DLTorrent = new DownloadTorrentFile();
}
public async void StartDownload(int torrentId)
{
try
{
DLTorrent.DecodeTorrent(torrentId);
//Creates a folder to the game
var newdic = Directory.CreateDirectory(savePath + torrentId);
fileInfo = new DirectoryInfo(savePath + torrentId);
//File info from a Directory
FileInfo[] files = fileInfo.GetFiles();
foreach (FileInfo i in files)
{
Console.WriteLine("Files exit " + i);
if (DLTorrent.GameInfomation[i.Name] != i.Length)
{
i.Delete();
isDelete = true;
}
else
{
Console.WriteLine("add files ");
ExistFile.Add(i.Name);
}
}
//Make a list which file not downloaded yet
var res = DLTorrent.GameInfomation.Keys.Except(ExistFile);
var nFiles = files.Length;
if(nFiles == 0 || !isDelete)
{
nFiles++;
foreach (var x in res)
{
((MainWindow)System.Windows.Application.Current.MainWindow).label1.Content = nFiles + " out of " + DLTorrent.GameInfomation.Keys.Count();
await DownloadProtocol("http://cdn.somewhere/rental/" + torrentId + "/" + x, savePath + torrentId + "/" + x);
nFiles++;
}
}
else
{
foreach (var x in res)
{
((MainWindow)System.Windows.Application.Current.MainWindow).label1.Content = nFiles + " out of " + DLTorrent.GameInfomation.Keys.Count();
await DownloadProtocol("http://cdn.somewhere/rental/" + torrentId + "/" + x, savePath + torrentId + "/" + x);
nFiles++;
}
}
}
catch
{
}
}
public async Task DownloadProtocol(string address, string location)
{
Uri Uri = new Uri(address);
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += (o, e) =>
{
Console.WriteLine(e.BytesReceived + " " + e.ProgressPercentage);
((MainWindow)System.Windows.Application.Current.MainWindow).DownloadBar.Value = e.ProgressPercentage;
};
client.DownloadFileCompleted += (o, e) =>
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
};
await client.DownloadFileTaskAsync(Uri, location);
}
}
}
你试过这个吗:
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
DownloadGameFile dlg = new DownloadGameFile();
await dlg.StartDownload(11825);
}
dlg.StartDownload
必须 return 一个任务。