- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我认为 Webclient.DownloadFileAysnc
会有一个默认超时,但是查看文档我无法在任何地方找到任何关于它的信息,所以我猜它没有。
我正在尝试像这样从 Internet 下载文件:
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += ((sender, args) =>
{
IndividualProgress = args.ProgressPercentage;
});
wc.DownloadFileCompleted += ((sender, args) =>
{
if (args.Error == null)
{
if (!args.Cancelled)
{
File.Move(filePath, Path.ChangeExtension(filePath, ".jpg"));
}
mr.Set();
}
else
{
ex = args.Error;
mr.Set();
}
});
wc.DownloadFileAsync(new Uri("MyInternetFile", filePath);
mr.WaitOne();
if (ex != null)
{
throw ex;
}
}
但是如果我关闭我的 WiFi(模拟互联网连接断开)我的应用程序只是暂停并且下载停止但它永远不会通过 DownloadFileCompleted
方法报告。
出于这个原因,我想在我的 WebClient.DownloadFileAsync
方法上实现超时。这可能吗?
顺便说一句,我使用的是 .Net 4,不想添加对第三方库的引用,所以不能使用 Async/Await
关键字
最佳答案
您可以使用 WebClient.DownloadFileAsync()。现在在计时器中,您可以像这样调用 CancelAsync():
System.Timers.Timer aTimer = new System.Timers.Timer();
System.Timers.ElapsedEventHandler handler = null;
handler = ((sender, args)
=>
{
aTimer.Elapsed -= handler;
wc.CancelAsync();
});
aTimer.Elapsed += handler;
aTimer.Interval = 100000;
aTimer.Enabled = true;
否则创建你自己的weclient
public class NewWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var req = base.GetWebRequest(address);
req.Timeout = 18000;
return req;
}
}
关于c# - 如何在 WebClient.DownloadFileAsync 上实现超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618452/
我正在使用 C# 中的 WebClient 下载文件。我正在使用 client.DownloadFileAsnyc()。在过去这工作得很好,任何异常都会被捕获并在完成处理程序中返回 (AsyncCom
我正在制作一个 WPF 应用程序。我使用 WebClient 的 DownloadFileAsync 下载文件。我一次从文件夹中下载每个文件。我第一次调用 DownloadProtocol 它工作正常
我正在尝试编写一个简单的应用程序,它可以从网络上下载一个文件。 class Program { static void Main(string[] args) { We
我需要使用 WebClient 下载文件在 PowerShell 2.0 中,我想显示下载进度,所以我这样做了: $activity = "Downloading" $client = New-Obj
WebClient DownloadFileAsync() 不适用于相同的 URl 和凭据... 有什么线索吗? static void Main(string[] args) {
我正在尝试使用 WPF 和 MVVM 从我的网络服务器下载一个大文件 (500 mb)。因此,以下属性都绑定(bind)到某种控件(进度条)。问题是,应用程序仍然挂起,即使在使用 DownloadFi
DownloadFileAsync 和 DownloadFileTaskAsync 有什么区别? 什么时候应该使用一个而不是另一个?任何例子将不胜感激。 最佳答案 一般模式 - 如果您发现两个名称以
我正在尝试通过以下方式在我的服务器上下载一个 zip 文件 WebClient.DownloadFileAsync(new Uri(DownloadLink),
正如您在代码中间看到的那样,有一个丑陋的线程阻塞代码等待文件下载并在命令行中启用进度报告。我的意思是,它仍然比 Thread.Sleep() 或忙等待要好,对吧?不管怎样,我知道 Wait/Pulse
这几乎就是标题中的全部问题。我有一个 WPF C# Windows 应用程序,我为用户下载文件,现在想显示速度。 最佳答案 mWebClient.DownloadProgressChanged +=
美好的一天。我正在使用 DownloadFileAsync 处理文件下载器类。在正常情况下,一切正常。但是当我下载文件并禁用网络连接时,下载进度会无限期停止,不会引发任何错误或调用任何回调。任何想法如
我正在使用下面的代码从 TFS 服务器下载多个附件: foreach (Attachment a in wi.Attachments) { WebClient wc = new WebC
我正在使用 WebClient 的 DownloadFileAsync 方法从服务器下载一些文件,在我对代码的非正式测试中我不禁注意到在 VS2010 中,它在启动时阻塞了大约 3 秒,在我看来,这首
所以我认为 Webclient.DownloadFileAysnc 会有一个默认超时,但是查看文档我无法在任何地方找到任何关于它的信息,所以我猜它没有。 我正在尝试像这样从 Internet 下载文件
描述 使用 webclient 的 DownloadFileAsync 下载多个文件,并利用文本文件输入 URL 进行下载。 问题 我使用的方法根本不会下载文件。只是运行,什么也不做。它填充列表数组然
安全取消 DownloadFileAsync 操作的最佳方法是什么? 我有一个线程(后台 worker )开始下载并管理它的其他方面,当我看到线程有 CancellationPending == tr
所以我正在使用 webClient.DownloadFileAsync(url, sFilePathToWriteFileTo); 下载文件 网址是string url = "http://local
所以基本上我的下载文件是: public void DownloadFile() { settings_btn.Enabled = false; label1.Text = "Chec
我正在使用 WebClient 按以下方式从 Internet 下载一些文件: try { ManualResetEvent mr = new ManualResetEvent(false);
我有此 C# 代码,但最终的 esi.zip 结果为 0 长度或基本上为空。该 URL 确实存在并确认手动下载文件。我很困惑买这个。 string zipPath = @"C:\download\es
我是一名优秀的程序员,十分优秀!