using System; using System.ComponentModel; using System.Net; using System.Windows.Forms; namespace VictusLauncher { public class WebClientEx : WebClient { private Int64 LastProgressUpdate { get; set; } private Byte[] DataResult { get; set; } private String StringResult { get; set; } protected override void OnDownloadProgressChanged(DownloadProgressChangedEventArgs e) { LastProgressUpdate = Timing.GetTimestamp(); base.OnDownloadProgressChanged(e); } protected override void OnDownloadFileCompleted(AsyncCompletedEventArgs e) { LastProgressUpdate = 0; base.OnDownloadFileCompleted(e); } protected override void OnDownloadDataCompleted(DownloadDataCompletedEventArgs e) { DataResult = e.Result; LastProgressUpdate = 0; base.OnDownloadDataCompleted(e); } protected override void OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) { StringResult = e.Result; LastProgressUpdate = 0; base.OnDownloadStringCompleted(e); } public void DownloadFileAsyncTimeout(Uri address, string fileName) { if (IsBusy) { throw new NotSupportedException(); } DownloadFileAsync(address, fileName); LastProgressUpdate = Timing.GetTimestamp(); while (IsBusy) { Timing.Sleep(1); Application.DoEvents(); if (Timing.DeltaSeconds(LastProgressUpdate, Timing.GetTimestamp()) <= 20 || LastProgressUpdate == 0) { continue; } TimeoutAsync(); } Int64 tUpdate = Timing.GetTimestamp(); while (LastProgressUpdate != 0) { Timing.Sleep(1); Application.DoEvents(); if (Timing.DeltaSeconds(tUpdate, Timing.GetTimestamp()) >= 5) { TimeoutAsync(); } } } public Byte[] DownloadDataAsyncTimeout(Uri address) { if (IsBusy) { throw new NotSupportedException(); } DownloadDataAsync(address); LastProgressUpdate = Timing.GetTimestamp(); while (IsBusy) { Timing.Sleep(1); Application.DoEvents(); if (Timing.DeltaSeconds(LastProgressUpdate, Timing.GetTimestamp()) <= 20 || LastProgressUpdate == 0) { continue; } TimeoutAsync(); } Int64 tUpdate = Timing.GetTimestamp(); while (LastProgressUpdate != 0) { Timing.Sleep(1); Application.DoEvents(); if (Timing.DeltaSeconds(tUpdate, Timing.GetTimestamp()) >= 5) { TimeoutAsync(); } } return DataResult; } public String DownloadStringAsyncTimeout(Uri address) { if (IsBusy) { throw new NotSupportedException(); } DownloadStringAsync(address); LastProgressUpdate = Timing.GetTimestamp(); while (IsBusy) { Timing.Sleep(1); Application.DoEvents(); if (Timing.DeltaSeconds(LastProgressUpdate, Timing.GetTimestamp()) <= 20 || LastProgressUpdate == 0) { continue; } TimeoutAsync(); } Int64 tUpdate = Timing.GetTimestamp(); while (LastProgressUpdate != 0) { Timing.Sleep(1); Application.DoEvents(); if (Timing.DeltaSeconds(tUpdate, Timing.GetTimestamp()) >= 5) { TimeoutAsync(); } } return StringResult; } public void TimeoutAsync() { CancelAsync(); throw new TimeoutException(); } } }