Click here to Skip to main content
6,595,444 members and growing! (20,417 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

UI for Simple HTTP File Downloader

By Fred Johanns

Demonstrates how to properly multi thread a UI application while downloading files over HTTP
C#, Windows, Visual Studio, Dev
Posted:18 Aug 2006
Views:24,762
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.61 Rating: 4.27 out of 5
1 vote, 14.3%
1

2

3
3 votes, 42.9%
4
3 votes, 42.9%
5
Sample Image - HttpFileDownloadUI.jpg

Introduction

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("http://www.codeproject.com/cs/internet/
                    CoolDownloader/CoolDownloader_demo.zip",
                    @"C:\Temp"));
                urls.Add(new DownloadURL("http://releases.mozilla.org/pub/mozilla.org/
                    firefox/releases/1.5.0.6/win32/en-US/Firefox Setup 1.5.0.6.exe",
                    @"C:\Temp\newname.zip"));
FileDownloaderForm downloader = new FileDownloaderForm(urls);
downloader.ShowDialog();

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)
{
    try
    {
        //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)
    {
        DownloaderExceptionManager.Publish(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);

History

  • First posted edition: 8/18/2006

License

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

About the Author

Fred Johanns


Member
I work at a small software company in Vernon, CT developing an enterprise class application built on .net Web Services and WinForms. I love it. If you don't do what you love, especially in this industry, you might as well quit now!
Occupation: Team Leader
Company: East Point Systems
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Generalc++ Pinmembermonsieur_jj18:22 8 May '08  
GeneralRe: c++ PinmemberFred Johanns4:21 9 May '08  
GeneralError in "public static void Publish(Exception ex)" PinmemberAndrey Kaplun8:05 5 Mar '07  
GeneralRe: Error in "public static void Publish(Exception ex)" PinmemberAndrey Kaplun10:03 8 Mar '07  
GeneralRe: Error in "public static void Publish(Exception ex)" PinmemberFred Johanns3:37 14 Mar '07  
GeneralThanks! Pinmembercs200x23:36 19 Dec '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Aug 2006
Editor: Deeksha Shenoy
Copyright 2006 by Fred Johanns
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project