Created
July 27, 2014 21:05
-
-
Save trcio/5e3303c5704978756bce to your computer and use it in GitHub Desktop.
Adds speed/time remaining to the ProgressChangedEventArgs of a webclient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
namespace SpeedClient | |
{ | |
public delegate void SpeedyDownloadProgressChangedEventHandler(SpeedyDownloadProgressChangedEventArgs e); | |
public delegate void SpeedyUploadProgressChangedEventHandler(SpeedyUploadProgressChangedEventArgs e); | |
public sealed class SpeedyClient : WebClient | |
{ | |
private Stopwatch Counter; | |
public event SpeedyDownloadProgressChangedEventHandler SpeedyDownloadProgressChanged; | |
public event SpeedyUploadProgressChangedEventHandler SpeedyUploadProgressChanged; | |
public SpeedyClient() | |
{ | |
Counter = new Stopwatch(); | |
} | |
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result) | |
{ | |
Counter.Reset(); | |
Counter.Start(); | |
return base.GetWebResponse(request, result); | |
} | |
protected override void OnDownloadProgressChanged(DownloadProgressChangedEventArgs e) | |
{ | |
base.OnDownloadProgressChanged(e); | |
if (SpeedyDownloadProgressChanged == null) return; | |
SpeedyDownloadProgressChanged(new SpeedyDownloadProgressChangedEventArgs(e, Counter.ElapsedMilliseconds)); | |
} | |
protected override void OnUploadProgressChanged(UploadProgressChangedEventArgs e) | |
{ | |
base.OnUploadProgressChanged(e); | |
if (SpeedyUploadProgressChanged == null) return; | |
SpeedyUploadProgressChanged(new SpeedyUploadProgressChangedEventArgs(e, Counter.ElapsedMilliseconds)); | |
} | |
} | |
public class SpeedyDownloadProgressChangedEventArgs : ProgressChangedEventArgs | |
{ | |
public SpeedyDownloadProgressChangedEventArgs(DownloadProgressChangedEventArgs e, long elapsed) : base(e.ProgressPercentage, e.UserState) | |
{ | |
BytesReceived = e.BytesReceived; | |
TotalBytesToReceive = e.TotalBytesToReceive; | |
BytesLeft = TotalBytesToReceive - BytesReceived; | |
BytesPerMillisecond = BytesReceived / elapsed; | |
TotalMillisecondsLeft = BytesLeft / BytesPerMillisecond; | |
} | |
public long BytesReceived { get; private set; } | |
public long TotalBytesToReceive { get; private set; } | |
public long BytesLeft { get; private set; } | |
public long BytesPerMillisecond { get; private set; } | |
public long TotalMillisecondsLeft { get; private set; } | |
} | |
public class SpeedyUploadProgressChangedEventArgs : ProgressChangedEventArgs | |
{ | |
public SpeedyUploadProgressChangedEventArgs(UploadProgressChangedEventArgs e, long elapsed) : base(e.ProgressPercentage, e.UserState) | |
{ | |
BytesReceived = e.BytesReceived; | |
BytesSent = e.BytesSent; | |
TotalBytesToReceive = e.TotalBytesToReceive; | |
TotalBytesToSend = e.TotalBytesToSend; | |
BytesLeft = TotalBytesToSend - BytesSent; | |
BytesPerMillisecond = BytesSent / elapsed; | |
TotalMillisecondsLeft = BytesLeft / BytesPerMillisecond; | |
} | |
public long BytesReceived { get; private set; } | |
public long BytesSent { get; private set; } | |
public long TotalBytesToReceive { get; private set; } | |
public long TotalBytesToSend { get; private set; } | |
public long BytesLeft { get; private set; } | |
public long BytesPerMillisecond { get; private set; } | |
public long TotalMillisecondsLeft { get; private set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment