简体   繁体   English

WebClient.DownloadFileTaskAsync()从未实际超时吗?

[英]Does WebClient.DownloadFileTaskAsync() never actually timeout?

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. 在异步前的日子里,人们想知道如何在WebClient上设置超时,答案只是扩展基类并覆盖GetWebRequest()并在那里设置超时。

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. 假设人们需要更长的超时时间。

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

So I guess the assumption was that people needed the request to end after a given time. 所以我猜这个假设是人们需要在给定时间后结束请求。

So does this mean that DownloadFileTaskAsync() or DownloadStringTaskAsync() never timeout by themselves ? 那么这是否意味着 DownloadFileTaskAsync()DownloadStringTaskAsync() 永远不会超时 Isn't a timeout failure an inherent part of any network operation? 超时故障不是任何网络操作的固有部分吗?

I've used the GetWebRequest() override to set a very small timeout value. 我已经使用GetWebRequest()重写来设置一个非常小的超时值。 It throws the timeout exception when non-async methods are called, but not when the async methods are called. 它会在调用非异步方法时抛出超时异常,但在调用异步方法时则不会。

I've decompiled the System.Net library, but the async methods seem to invoke some cached anonymous lambdas that are not easily discoverable. 我已经反编译了System.Net库,但异步方法似乎调用了一些不容易被发现的缓存匿名lambdas。

Does anyone know with certainty if the DownloadXyzTaskAsync() methods execute with the equivalent of an infinite timeout value? 有没有人确切知道DownloadXyzTaskAsync()方法是否以相当于无限超时值的方式执行?

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. 等待可以是无限的(如果操作本身没有结束则是无限的),或者等待可以接收某种超时,之后它会解除阻塞和超时。

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 (在超时后可能会或可能不会通过计时器发出信号)。

So, this (and any other kind of) asynchronous operation needs something (ie a CancellationToken ) to be able to timeout. 因此,这种(以及任何其他类型的)异步操作需要某些东西(即CancellationToken )能够超时。 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 . 确实,库可以在内部使用计时器,但很少在.Net中进行,因为它是意外的,你可以自己取消CancellationToken

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. SocketTcpClientUdpClient等也是如此。

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 . 现在,如果您想要从HttpWebRequest的实际代码中进行确认,您可以看到超时用于在此处创建计时器队列。 That queue is used to create a timeout timer in GetResponse but never in BeginGetResponse . 该队列用于在GetResponse创建超时计时器,但从BeginGetResponse That is the asynchronous option used in the DownloadXXXAsync which is used in DownloadXXXTaskAsync . 这是在使用异步选项DownloadXXXAsync这是在使用DownloadXXXTaskAsync

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM