time-out problem of WebClient and its solution
Transferred from: Http://blog.163.com/[email protected]/blog/static/62440288201112245345838/
WebClient cannot set the request time-out time and the request read-write timeout when downloading the request. WebClient when an asynchronous download encounters a problem such as a network failure, no response time-out causes the app to hang.
1.Webclient Request Timeout setting
Override WebClient's GetWebRequest method, add request time-outs and read-write timeouts for HttpWebRequest
protected override WebRequest GetWebRequest (Uri address)
{
HttpWebRequest request = (HttpWebRequest) base. GetWebRequest (address);
Request. Timeout = $ * timeout;
Request. Readwritetimeout = + * TIMEOUT;
return request;
}
2.WebClient in asynchronous download
Create a timer to monitor the response and cancel the download when it expires
public class Calculagraph
{
<summary>
Time to Event
</summary>
public event Timeoutcaller Timeover;
<summary>
Start time
</summary>
Private DateTime _starttime;
Private TimeSpan _timeout = new TimeSpan (0, 0, 10);
private bool _hasstarted = false;
Object _userdata;
<summary>
Timer Construction Method
</summary>
<param name= "UserData" > User data callback at the end of the timer </param>
Public Calculagraph (Object UserData)
{
Timeover + = new Timeoutcaller (ontimeover);
_userdata = UserData;
}
<summary>
Timeout exit
</summary>
<param name= "UserData" ></param>
public virtual void Ontimeover (object UserData)
{
Stop ();
}
<summary>
Expiration Time (seconds)
</summary>
public int Timeout
{
Get
{
Return _timeout. Seconds;
}
Set
{
if (value <= 0)
Return
_timeout = new TimeSpan (0, 0, value);
}
}
<summary>
Have you started the timekeeping
</summary>
public bool Hasstarted
{
Get
{
return _hasstarted;
}
}
<summary>
Start timing
</summary>
public void Start ()
{
Reset ();
_hasstarted = true;
Thread th = new Thread (waitcall);
Th. IsBackground = true;
Th. Start ();
}
<summary>
Reset
</summary>
public void Reset ()
{
_starttime = DateTime.Now;
}
<summary>
Stop timing
</summary>
public void Stop ()
{
_hasstarted = false;
}
<summary>
Check whether it expires
</summary>
<returns></returns>
private bool Checktimeout ()
{
Return (DateTime.Now-_starttime). Seconds >= Timeout;
}
private void Waitcall ()
{
Try
{
Whether cyclic detection expires
while (_hasstarted &&!checktimeout ())
{
Thread.Sleep (1000);
}
if (timeover! = null)
Timeover (_userdata);
}
catch (Exception)
{
Stop ();
}
}
}
// <summary>
Callback delegate when expired
</summary>
<param name= "UserData" ></param>
public delegate void Timeoutcaller (object userdata);
public class Cnnwebclient:webclient
{
Private Calculagraph _timer;
private int _timeout = 10;
<summary>
Expiry time
</summary>
public int Timeout
{
Get
{
return _timeout;
}
Set
{
if (value <= 0)
_timeout = 10;
_timeout = value;
}
}
<summary>
Override GetWebRequest, add WebRequest object timeout time
</summary>
<param name= "Address" ></param>
<returns></returns>
protected override WebRequest GetWebRequest (Uri address)
{
HttpWebRequest request = (HttpWebRequest) base. GetWebRequest (address);
Request. Timeout = $ * timeout;
Request. Readwritetimeout = + * TIMEOUT;
return request;
}
<summary>
Downloads with Expiration Timings
</summary>
public void Downloadfileasyncwithtimeout (Uri address, string fileName, Object usertoken)
{
if (_timer = = null)
{
_timer = new Calculagraph (this);
_timer. Timeout = timeout;
_timer. Timeover + = new Timeoutcaller (_timer_timeover);
This. DownloadProgressChanged + = new Downloadprogresschangedeventhandler (cnnwebclient_downloadprogresschanged);
}
DownloadFileAsync (Address, fileName, usertoken);
_timer. Start ();
}
<summary>
WebClient download Process event, raised when data is received
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
void Cnnwebclient_downloadprogresschanged (object sender, DownloadProgressChangedEventArgs e)
{
_timer. Reset ();//reset Timer
}
///&NBSP;<SUMMARY>
/// Timer expires
/// </summary>
/// <param name= " UserData "></PARAM>
void _timer_timeover ( Object userdata)
{
this. CancelAsync ();//Cancel Download
}
}