Click here to Skip to main content
15,439,373 members
Articles / Programming Languages / C#
Posted 18 Aug 2006


50 bookmarked

UI for Simple HTTP File Downloader

Rate me:
Please Sign up or sign in to vote.
4.13/5 (8 votes)
18 Aug 2006CPOL1 min read
Demonstrates how to properly multi thread a UI application while downloading files over HTTP
Sample Image - HttpFileDownloadUI.jpg


This is a simple user interface for downloading files via HTTP based on Phil Crosby's article located here.

Using the Code

The solution is located in the FileDownloader directory and contains two projects: one is the FileDownlaoder UI library and the other is the WinForms test application. The code leaves much room for improvement, but as is usually the case in this field, the due date for my current project is yesterday; hence it's taken me so long to get a spare minute to post this! Here is how to instantiate the downloader:

DownloadURLCollection urls = new DownloadURLCollection();
                urls.Add(new DownloadURL("
                urls.Add(new DownloadURL("
                    firefox/releases/ Setup",
FileDownloaderForm downloader = new FileDownloaderForm(urls);

That's it! No fuss, no muss. What happens under the hood is very well explained in Phil's article.

Points of Interest

Before the world of the BackgroundWorker object in .NET 2.0, in order to get a multi-threaded application to interact with the user interface, you had to invoke the worker threads request on to the UI thread by hand:

private void downloader_ProgressChanged(object sender, DownloadEventArgs e)
        //since the downloader was started on a different thread, it must be invoked
        //back on to the UI thread before we interact with the UI controls
        this.Invoke(new MarshalProgress(this.MarshaledProgressChanged), new object[]{
                    sender, e});
    catch(Exception ex)

When the progress event is fired, it's fired on a different thread. This is so while the file is downloading, the user interface window remains responsive. Before changing the value of the progress bar or the text of a label, the call must be marshaled to the UI thread by calling Invoke on the control that exists on the UI thread, in this case, the FileDownloaderForm. Thus, this.Invoke(...). In the invoke method, you pass a delegate, which is basically an object that can be thought of as a pointer to a function: it "points" to a specific function that conforms to the parameters described by the delegate and returns the value described by the delegate:

delegate void MarshalProgress(object sender, DownloadEventArgs e);


  • First posted edition: 8/18/2006


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Team Leader
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Generalc++ Pin
monsieur_jj8-May-08 17:22
Membermonsieur_jj8-May-08 17:22 
GeneralRe: c++ Pin
Fred Johanns9-May-08 3:21
MemberFred Johanns9-May-08 3:21 
GeneralError in "public static void Publish(Exception ex)" Pin
Andrey Kaplun5-Mar-07 7:05
MemberAndrey Kaplun5-Mar-07 7:05 
GeneralRe: Error in "public static void Publish(Exception ex)" Pin
Andrey Kaplun8-Mar-07 9:03
MemberAndrey Kaplun8-Mar-07 9:03 
GeneralRe: Error in "public static void Publish(Exception ex)" Pin
Fred Johanns14-Mar-07 2:37
MemberFred Johanns14-Mar-07 2:37 
GeneralThanks! Pin
cs200x19-Dec-06 22:36
Membercs200x19-Dec-06 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.