使用 WebClient 异步下载文件不起作用
Download file async using WebClient doens't work
我正在使用 C# 在 VS15 中制作一个控制台应用程序。
这是我的下载 class:
class DownloadGamefile
{
public void DownloadFile(string address, string location)
{
WebClient client = new WebClient();
Uri Uri = new Uri(address);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
client.DownloadFileAsync(Uri, location);
}
private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
}
}
这是我的主要内容:
class Program
{
static void Main(string[] args)
{
DownloadGamefile DGF = new DownloadGamefile();
DGF.DownloadFile("URL", @"C:\Users\LocDaiLe\Desktop\file.file");
}
}
文件出现在正确的文件夹中,但大小为 0 字节并且我的控制台没有显示任何下载进度。
由于异步调用不会阻塞当前线程,我的猜测是 DownloadFileAsync (https://msdn.microsoft.com/en-us/library/ms144196(v=vs.110).aspx) return 立即导致您的 DownloadFile 也立即 return。这将在文件完成下载之前结束程序执行。
您需要阻塞线程,直到文件下载完成。您是否考虑过使用 .NET 4.5 (https://msdn.microsoft.com/en-us/library/hh191443.aspx) 中的 async/await?您还可以使用 Progress/Completed 事件来同步您的线程,以便应用程序在文件完成之前不会退出。 Console.ReadLine() 也应该阻塞当前线程,直到按下 Enter。
我扩展了您的代码以支持线程阻塞。这将使主线程在检查之间休眠 1 秒,直到文件完成。我使用关键字 "volatile" (https://msdn.microsoft.com/en-us/library/x13ttww7.aspx) 来确保始终抓取最新值。这对于多线程很重要。 _completed = true 在 if/else 的外面,因为我们希望它即使被取消也能退出。如果需要,您可以扩展此解决方案以处理取消的下载。
class DownloadGamefile
{
private volatile bool _completed;
public void DownloadFile(string address, string location)
{
WebClient client = new WebClient();
Uri Uri = new Uri(address);
_completed = false;
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
client.DownloadFileAsync(Uri, location);
}
public bool DownloadCompleted { get { return _completed; } }
private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
_completed = true;
}
}
class Program
{
static void Main(string[] args)
{
DownloadGamefile DGF = new DownloadGamefile();
DGF.DownloadFile("URL", @"C:\Users\LocDaiLe\Desktop\file.file");
while (!DGF.DownloadCompleted)
Thread.Sleep(1000);
}
}
我正在使用 C# 在 VS15 中制作一个控制台应用程序。 这是我的下载 class:
class DownloadGamefile
{
public void DownloadFile(string address, string location)
{
WebClient client = new WebClient();
Uri Uri = new Uri(address);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
client.DownloadFileAsync(Uri, location);
}
private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
}
}
这是我的主要内容:
class Program
{
static void Main(string[] args)
{
DownloadGamefile DGF = new DownloadGamefile();
DGF.DownloadFile("URL", @"C:\Users\LocDaiLe\Desktop\file.file");
}
}
文件出现在正确的文件夹中,但大小为 0 字节并且我的控制台没有显示任何下载进度。
由于异步调用不会阻塞当前线程,我的猜测是 DownloadFileAsync (https://msdn.microsoft.com/en-us/library/ms144196(v=vs.110).aspx) return 立即导致您的 DownloadFile 也立即 return。这将在文件完成下载之前结束程序执行。
您需要阻塞线程,直到文件下载完成。您是否考虑过使用 .NET 4.5 (https://msdn.microsoft.com/en-us/library/hh191443.aspx) 中的 async/await?您还可以使用 Progress/Completed 事件来同步您的线程,以便应用程序在文件完成之前不会退出。 Console.ReadLine() 也应该阻塞当前线程,直到按下 Enter。
我扩展了您的代码以支持线程阻塞。这将使主线程在检查之间休眠 1 秒,直到文件完成。我使用关键字 "volatile" (https://msdn.microsoft.com/en-us/library/x13ttww7.aspx) 来确保始终抓取最新值。这对于多线程很重要。 _completed = true 在 if/else 的外面,因为我们希望它即使被取消也能退出。如果需要,您可以扩展此解决方案以处理取消的下载。
class DownloadGamefile
{
private volatile bool _completed;
public void DownloadFile(string address, string location)
{
WebClient client = new WebClient();
Uri Uri = new Uri(address);
_completed = false;
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
client.DownloadFileAsync(Uri, location);
}
public bool DownloadCompleted { get { return _completed; } }
private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
_completed = true;
}
}
class Program
{
static void Main(string[] args)
{
DownloadGamefile DGF = new DownloadGamefile();
DGF.DownloadFile("URL", @"C:\Users\LocDaiLe\Desktop\file.file");
while (!DGF.DownloadCompleted)
Thread.Sleep(1000);
}
}