WEBSITE

Download Web Content Asynchronously

9/26/2010 7:43:58 PM
Here’s a rundown of how the asynchronous model works in this instance:
  • Listen to events that will notify you of the download status.

  • Start the download event with an -Async method.

  • Do other stuff (even if it’s just listen for a button click to cancel).

  • In the event handlers for the download events, respond to the download progress (or completion).

Listing 1 shows a portion of the code for our sample application.

Listing 1. Asynchronous Web Downloader
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
}
}
}


Other  
 
Most View
Samsung WB250F – A Camera With Built-In Wi-Fi (Part 1)
Lenovo Miix - A Tablet With A Powerful Quad-Core Processor
Samsung GALAXY Camera - The World's First Smart Camera
Apple Mac Mini Desktop Computer
What To Do With An Old Mac (Part 2)
GeForce GTX 660 Graphics Cards Roundup (Part 5)
Blackberry Z10 - It’s Up Against Some Tough Competition
System Center Configuration Manager 2007 : Creating a Package (part 4) - Forefront Client - Using the New Package Wizard
Apple iPhone 5 - An Outstanding Handset
Microsoft ASP.NET 3.5 : Caching Application Data (part 2) - Working with the ASP.NET Cache
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered