Introduction:
In this article,i am going to explain about how to download a webcontent asynchronously using asp.net/C# 4.0.
Main:
Commonly we know,Asynchronous operations allow you to view progress and give the user a chance to cancel the operation.
Here we are going to create One sample asynchronous model,It will works
1.Listen to events that will notify you of the download status.
2.Start the download event with an -Async method.
3.Do other stuff (even if it’s just listen for a button click to cancel).
4.In the event handlers for the download events, respond to the download progress
(or completion).
Code for Sample asynchronous model ,
using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WebDownloaderAsync
{
public partial class Form1 : Form
{
WebClient _client = null;
bool _downloading = false;//for tracking what button does
public Form1()
{
InitializeComponent();
}
private void buttonDownload_Click(object sender, EventArgs e)
{
if (!_downloading)
{
_client = new WebClient();
//listen for events so we know when things happen
_client.DownloadProgressChanged +=
_client_DownloadProgressChanged;
_client.DownloadDataCompleted +=
_client_DownloadDataCompleted;
try
{
//start downloading and immediately return
_client.DownloadDataAsync(new Uri(textBoxUrl.Text));
//now our program can do other stuff while we wait!
_downloading = true;
buttonDownload.Text = “Cancel”;
}
catch (UriFormatException ex)
{
MessageBox.Show(ex.Message);
_client.Dispose();
}
catch (WebException ex)
{
MessageBox.Show(ex.Message);
_client.Dispose();
}
}
else
{
_client.CancelAsync();
}
}
void _client_DownloadProgressChanged(object sender,
DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
labelStatus.Text =
string.Format(“{0:N0} / {1:N0} bytes received”,
e.BytesReceived,
e.TotalBytesToReceive);
}
void _client_DownloadDataCompleted(object sender,
?DownloadDataCompletedEventArgs e)
{
//now the file is done downloading
if (e.Cancelled)
{
progressBar.Value = 0;
labelStatus.Text = “Cancelled”;
}
else if (e.Error != null)
{
progressBar.Value = 0;
labelStatus.Text = e.Error.Message;
}
else
{
progressBar.Value = 100;
labelStatus.Text = “Done!”;
}
//don’t forget to dispose our download client!
_client.Dispose();
_downloading = false;
buttonDownload.Text = “Download”;
//access data in e.Result
}
}
}
using System; using System.ComponentModel; using System.Text; using System.Windows.Forms; using System.Net; namespace WebDownloaderAsync { public partial class Form1 : Form { WebClient _client = null; bool _downloading = false;//for tracking what button does public Form1() { InitializeComponent(); } private void buttonDownload_Click(object sender, EventArgs e) { if (!_downloading) { _client = new WebClient(); //listen for events so we know when things happen _client.DownloadProgressChanged += _client_DownloadProgressChanged; _client.DownloadDataCompleted += _client_DownloadDataCompleted; try { //start downloading and immediately return _client.DownloadDataAsync(new Uri(textBoxUrl.Text)); //now our program can do other stuff while we wait! _downloading = true; buttonDownload.Text = “Cancel”; } catch (UriFormatException ex) { MessageBox.Show(ex.Message); _client.Dispose(); } catch (WebException ex) { MessageBox.Show(ex.Message); _client.Dispose(); } } else { _client.CancelAsync(); } } void _client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { progressBar.Value = e.ProgressPercentage; labelStatus.Text = string.Format(“{0:N0} / {1:N0} bytes received”, e.BytesReceived, e.TotalBytesToReceive); } void _client_DownloadDataCompleted(object sender, ?DownloadDataCompletedEventArgs e) { //now the file is done downloading if (e.Cancelled) { progressBar.Value = 0; labelStatus.Text = “Cancelled”; } else if (e.Error != null) { progressBar.Value = 0; labelStatus.Text = e.Error.Message; } else { progressBar.Value = 100; labelStatus.Text = “Done!”; } //don’t forget to dispose our download client! _client.Dispose(); _downloading = false; buttonDownload.Text = “Download”; //access data in e.Result } } } |
Conclusion:
Enjoy this code,
Happy Coding.
Greetings! Keep up the good work. That was a interesting comment. I am ready to find out more about this particular topic. See ya. Greetings from France.
Greetings! Congrats. That was a fascinating post. I am ready to find out more about this fascinating line of information. See ya. Greetings from France.