Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C#

Multi-threaded file download manager

Rate me:
Please Sign up or sign in to vote.
4.79/5 (49 votes)
5 Jun 20064 min read 271K   15.7K   188  
A fully working multi-threaded file downloader application.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Xml;

namespace BloodHound
{
    class FileDownloadThread
    {
        private ThreadStart serviceDelegate;
        private Thread newT;
        /// <summary>
		/// The constructor takes more than one parameter
		/// as oppose to only one parameter is the bare bone Thread
		/// class. This allow we to perform some additional service
		/// based on the additional parameters, in our case, we will pass
		/// the configuration data that contains the user account information
		/// which is later used to perform the thread's security switch
		/// </summary>
		/// <param name="start">the ThreadStart delegate for the target method</param>
		/// <param name="xml">the configuraiton data contains the user account information</param>
        public FileDownloadThread(ThreadStart start, XmlNode xml)
		{
			serviceDelegate = start;
			runAs = xml;
			//create a new thread that calls the WrappingMethod
			newT = new Thread(new ThreadStart(WrappingMethod));
		}
		public void Start()
		{
			//start the thread.
			newT.Start();
		}

		/// <summary>
		/// WrappingMethod wraps performs the security
		/// switch and then invokes the ThreadStart delegate.
		/// </summary>
		private void WrappingMethod()
		{
			//retrieve the user account information to which thread is
			//switched.
			bool inheritIdentity = Boolean.Parse(runAs.Attributes["InheritIdentity"].Value);
			if (inheritIdentity == false)
			{
				string userid = runAs.SelectSingleNode("User").InnerText;
				string password= runAs.SelectSingleNode("Password").InnerText;
				string domain = runAs.SelectSingleNode("Domain").InnerText;
				//call the utility class to switch the current thread's security context.
				SecurityUtility su = new SecurityUtility();
				su.Switch(userid, password,domain);
			}
			//invoke the ThreadStart delegate object passed in 
			//on the class constructor
			serviceDelegate();
		}

		/// <summary>
		/// BaseThread property provide access to the 
		/// underlying thread, which allow external program
		/// to control the thread.
		/// </summary>
		public Thread BaseThread
		{
			get
			{
				return newT;
			}
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions