WebClient.DownloadFileTaskAsync()永远不会真正超时吗? [英] Does WebClient.DownloadFileTaskAsync() never actually timeout?

查看:130
本文介绍了WebClient.DownloadFileTaskAsync()永远不会真正超时吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在异步之前的日子里,人们想知道如何在 WebClient 上设置超时,答案只是扩展基类并覆盖 GetWebRequest()并在那里设置超时时间.

In the pre-async days, people wanted to know how to set the timeout on WebClient and the answer was simply to extend the base class and override GetWebRequest() and set the timeout there.

protected override WebRequest GetWebRequest(Uri address)
{
    // NOTE: this override has no affect if the Async methods are used!!!
    WebRequest request = base.GetWebRequest(address);
    ((HttpWebRequest)request).Timeout = 20 * 60 * 1000;
    ((HttpWebRequest)request).ReadWriteTimeout = 20 * 60 * 1000;
    return request;
}

假定人们需要更长的时间.

The assumption was that people needed a longer timeouts.

然后,通过添加 xyzTaskAsync()方法,人们想知道如何设置超时,答案是使用由本地计时器驱动的CancellationToken.

Then with the addition of the xyzTaskAsync() methods, people wanted to know how to set the timeout, and the answer was to use a CancellationToken driven by a local timer.

所以我想假设是人们需要在给定时间后结束请求.

So I guess the assumption was that people needed the request to end after a given time.

所以这是否意味着 DownloadFileTaskAsync() DownloadStringTaskAsync() 从不超时??超时失败不是任何网络操作的固有部分吗?

So does this mean that DownloadFileTaskAsync() or DownloadStringTaskAsync() never timeout by themselves? Isn't a timeout failure an inherent part of any network operation?

我使用了 GetWebRequest()重写来设置非常小的超时值.当调用非异步方法时,它将引发超时异常,但在调用异步方法时,则不会引发超时异常.

I've used the GetWebRequest() override to set a very small timeout value. It throws the timeout exception when non-async methods are called, but not when the async methods are called.

我已经反编译了System.Net库,但是异步方法似乎会调用一些不容易发现的缓存匿名lambda.

I've decompiled the System.Net library, but the async methods seem to invoke some cached anonymous lambdas that are not easily discoverable.

有人可以肯定地知道 DownloadXyzTaskAsync()方法是否以等效的无限超时值执行吗?

Does anyone know with certainty if the DownloadXyzTaskAsync() methods execute with the equivalent of an infinite timeout value?

推荐答案

同步操作被阻塞,这意味着线程在某种等待句柄上被阻塞.该等待可以是无限制的(如果操作本身没有结束,则可以是无限的),或者该等待可以收到某种超时,然后解除阻塞和超时.

Synchronous operations are blocking, that means a thread is blocked on some kind of wait handle. That wait can be unlimited (and so infinite if the operation itself doesn't end) or that wait can receive some kind of timeout after which it unblocks and timeouts.

异步操作本质上是异步的,它没有任何活动的部分在做某事.没有线程被阻塞或类似的东西.这意味着它本身在没有任何提示的情况下就无法真正超时或取消,即使这样,该操作也只是被放弃而不被取消.通常是 CancellationToken (可能在超时后用计时器发出信号).

An asynchronous operation, by inherently being asynchronous, doesn't have any active part doing something. There's no thread being blocked or anything similar. That means that it inherently can't really timeout or cancel without something telling it to, and even then the operation is merely abandoned and not cancelled. That something is usually a CancellationToken (that may or may not be signaled with a timer after a timeout).

因此,此(以及任何其他类型的)异步操作需要某些内容(即 CancellationToken )才能超时.确实,该库可以在内部使用计时器,但是在.Net中很少这样做,因为这是意外的,您可以自己使用自动取消的 CancellationToken 来做到这一点.

So, this (and any other kind of) asynchronous operation needs something (i.e. a CancellationToken) to be able to timeout. It's true that the library can use a timer internally but that is rarely done in .Net as it's unexpected and you can do that yourself with a self cancelling CancellationToken.

因此,在这种特定情况下,通常,异步方法通常不受配置的超时影响. Socket TcpClient UdpClient 等也是这种情况.

So, in this specific case and in general, async methods aren't usually affected by a configured timeout. That's also the case with Socket, TcpClient, UdpClient, etc.

现在,如果要从 HttpWebRequest 的实际代码中进行确认,您可以看到超时用于创建计时器队列 GetResponse ,但从不在 BeginGetResponse .这是 DownloadXXXAsync 中使用的异步选项,而 DownloadXXXTaskAsync 中使用的异步选项.

Now, if you want confirmation from the actual code of HttpWebRequest you can see that the timeout is used to create a timer queue here. That queue is used to create a timeout timer in GetResponse but never in BeginGetResponse. That is the asynchronous option used in the DownloadXXXAsync which is used in DownloadXXXTaskAsync.

这篇关于WebClient.DownloadFileTaskAsync()永远不会真正超时吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆