Skip to content

Instantly share code, notes, and snippets.

@brooklynDev
Created October 15, 2012 16:29
Show Gist options
  • Save brooklynDev/3893421 to your computer and use it in GitHub Desktop.
Save brooklynDev/3893421 to your computer and use it in GitHub Desktop.
Asynchronouse file download with WebRequest + show progress in Progress bar. APM vs async/await
#######################################
APM
#######################################
public partial class APMVersion : Form
{
private const int BUFFER_SIZE = 1024;
public Form1()
{
InitializeComponent();
}
private void ManipulateUI(Action action)
{
this.Invoke(action);
}
private void button1_Click(object sender, EventArgs e)
{
var url = this.textBox1.Text;
if (String.IsNullOrEmpty(url))
{
return;
}
this.button1.Enabled = false;
var webRequest = WebRequest.Create(url);
webRequest.BeginGetResponse(iar =>
{
var response = webRequest.EndGetResponse(iar);
var contentLength = (int)response.ContentLength;
ManipulateUI(() => this.progressBar1.Maximum = contentLength);
var stream = response.GetResponseStream();
var bytes = new byte[BUFFER_SIZE];
var state = new DownloadFileRequestState { Stream = stream, Bytes = bytes, Url = url };
stream.BeginRead(bytes, 0, BUFFER_SIZE, ReadCallback, state);
}, null);
}
private void ReadCallback(IAsyncResult iAsyncResult)
{
var state = (DownloadFileRequestState)iAsyncResult.AsyncState;
var stream = state.Stream;
var bytesRead = stream.EndRead(iAsyncResult);
if (bytesRead > 0)
{
this.ManipulateUI(() => this.progressBar1.Increment(bytesRead));
state.AllBytes.AddRange(state.Bytes.Take(bytesRead));
stream.BeginRead(state.Bytes, 0, BUFFER_SIZE, ReadCallback, state);
}
else
{
var fileName = Path.GetFileName(state.Url);
File.WriteAllBytes(fileName, state.AllBytes.ToArray());
this.ManipulateUI(() => this.button1.Enabled = true);
}
}
private class DownloadFileRequestState
{
public DownloadFileRequestState()
{
this.AllBytes = new List<byte>();
}
public List<byte> AllBytes { get; private set; }
public Stream Stream { get; set; }
public byte[] Bytes { get; set; }
public string Url { get; set; }
}
}
#######################################
async/await
#######################################
public partial class DownloadSpeedAsync : Form
{
private const int BUFFER_SIZE = 1024;
public DownloadSpeedAsync()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
var url = this.textBox1.Text;
if (String.IsNullOrEmpty(url))
{
return;
}
this.button1.Enabled = false;
var webRequest = WebRequest.Create(url);
var response = await webRequest.GetResponseAsync();
this.progressBar1.Maximum = (int)response.ContentLength;
var list = new List<byte>();
var bytes = new byte[BUFFER_SIZE];
var stream = response.GetResponseStream();
int bytesRead = 0;
do
{
bytesRead = await stream.ReadAsync(bytes, 0, BUFFER_SIZE);
list.AddRange(bytes.Take(bytesRead));
this.progressBar1.Increment(bytesRead);
} while (bytesRead > 0);
var fileName = Path.GetFileName(url);
File.WriteAllBytes(fileName, list.ToArray());
this.button1.Enabled = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment