Multithreading downloading via WebClient RRS feed

  • Question

  • I have a question, i am trying to implement a multithreaded method that will instaniate a new Thread for each item in a List<string> (names of the references download and add to the Assembly).

     Now i am having an issue trying to "wait" for the callback of the WebClient to finish before returning to the main UI thread to load the UserControl.

    Any pointers?

    I need to "wait" until all threads stop before returning to the parent method.

    Here is my code..

    private static List<string> _MainAssemblies = new List<string>() {"DLLOne.DLL", "DLLTwo.DLL", "DLLThree.DLL", "DLLFour.DLL", "DLLFive.DLL"};       
    public static bool LoadAssemblies()
                Thread thread = null;
                for (int i = 0; i < _MainAssemblies.Count; i++)
                    thread = new Thread(new ParameterizedThreadStart(Load));
                return true;
            static void Load(object Assembly)
                WebClient client = new WebClient();
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                client.OpenReadAsync(new Uri(Assembly.ToString(), UriKind.Relative));

            static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
                AssemblyPart part = new AssemblyPart();
                Assembly a = part.Load(e.Result);

    Wednesday, January 21, 2009 1:27 AM

All replies

  • Ok.

    Why do you want one thread by download.

    When you call OpenReadAsync the Async is important, it means your OpenRead will be asynchronous.

    So, I think you don't need to execute each WebClient in a separate thread.


    If you want absolutely many thread, you can use a counter and when your counter equals your number of thread, it means all of your thread are finish. Or maybe you can use Thread.Join() in each thread.

    Wednesday, January 21, 2009 1:41 AM
  • SkyRunner,

    If you are trying to download multiple files, you cannot use the same thread for the same webclient object.  I wanted to download ALL assemblies async in seperate threads.

    Wednesday, January 21, 2009 1:56 AM
  • You can use multiple WebClient.

     Check my other piece of reply.

    Wednesday, January 21, 2009 2:05 AM
  • Skyrunner,

     yes i know you can use multiple webclients, but you have to create those objects manually, which is where i wanted to use multiple threads for the same methods to download the appropriate files.

    Wednesday, January 21, 2009 2:48 AM
  • Ok, I insist but


    public static bool LoadAssemblies()
        for (int i = 0; i < _MainAssemblies.Count; i++)
            WebClient webclient = new WebClient();
            webclient.OpenReadCompleted += new OpenReadCompletedEventHandler(webclient_OpenReadCompleted);
            webclient.OpenWriteAsync(new Uri(_MainAssembliesIdea, UriKind.Relative);
        return true;


    You can use sender parameter in webclient_OpenReadCompleted for differentiate them if you need.

    And I repeat, check my other piece of reply in my first reply (with Thread.Join).

    Wednesday, January 21, 2009 3:01 AM
  • Skyrunner, you cannot "join" threads using the same object that has been instianted over and over again. (at least i dont think you can) and loading AssemblyParts via a different thread throws a Cross thread exception.

    Oh well, back to the drawing board.

    Wednesday, January 21, 2009 5:31 AM