Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WebClient and Events

WebClient and Events

Scheduled Pinned Locked Moved C#
helpcsharplinuxdebuggingquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Teuz
    wrote on last edited by
    #1

    Ok Guys I'm facing a strange problem here, and I dunno if its a bug or I am doing something wrong. Here's the code:

    using System;
    using System.Diagnostics;
    using System.Net;
    using System.Windows.Forms;

    namespace ConsoleApplication
    {
    internal class Program
    {
    private static void Main(string[] args)
    {
    var webClient = new WebClient();

            //var progressBar = new ProgressBar(); 
    
            webClient.DownloadProgressChanged += (o, e) => Debug.WriteLine(e.ProgressPercentage);
            webClient.DownloadFileCompleted += (o, e) => Debug.WriteLine("Download completed.");
    
            Debug.WriteLine("Starting download...");
            webClient.DownloadFileAsync(new Uri("http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-netinst.iso"), "debian-504-i386-netinst.iso");
    
            Console.Read();
        }
    }
    

    }

    Everything works fine like this, events are raised normally. Now try to uncomment the progressbar initialization and events will stop to be fired (I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens) even if the download proceeds normally. Maybe is just me or maybe I have convinced myself this code is right that I cant no longer see where I epic fail. Anyone having the same problem? Thanks in advance. Matt

    J 1 Reply Last reply
    0
    • T Teuz

      Ok Guys I'm facing a strange problem here, and I dunno if its a bug or I am doing something wrong. Here's the code:

      using System;
      using System.Diagnostics;
      using System.Net;
      using System.Windows.Forms;

      namespace ConsoleApplication
      {
      internal class Program
      {
      private static void Main(string[] args)
      {
      var webClient = new WebClient();

              //var progressBar = new ProgressBar(); 
      
              webClient.DownloadProgressChanged += (o, e) => Debug.WriteLine(e.ProgressPercentage);
              webClient.DownloadFileCompleted += (o, e) => Debug.WriteLine("Download completed.");
      
              Debug.WriteLine("Starting download...");
              webClient.DownloadFileAsync(new Uri("http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-netinst.iso"), "debian-504-i386-netinst.iso");
      
              Console.Read();
          }
      }
      

      }

      Everything works fine like this, events are raised normally. Now try to uncomment the progressbar initialization and events will stop to be fired (I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens) even if the download proceeds normally. Maybe is just me or maybe I have convinced myself this code is right that I cant no longer see where I epic fail. Anyone having the same problem? Thanks in advance. Matt

      J Offline
      J Offline
      johannesnestler
      wrote on last edited by
      #2

      Hi, this looks strange! I'd guess your problem in the example is Console.Read()... When I tried your code in an windows forms project there was no problem. Can you show your orignal code

      Teuz wrote:

      (I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens)

      T 1 Reply Last reply
      0
      • J johannesnestler

        Hi, this looks strange! I'd guess your problem in the example is Console.Read()... When I tried your code in an windows forms project there was no problem. Can you show your orignal code

        Teuz wrote:

        (I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens)

        T Offline
        T Offline
        Teuz
        wrote on last edited by
        #3

        Thank you for the reply. How did you managed to make it work in a windows form project? This code acts exactly like the console one, no event are fired but the file is downloaded:

        using System;
        using System.Diagnostics;
        using System.Net;
        using System.Threading;
        using System.Windows.Forms;

        namespace WindowsFormsApplication1
        {
        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();
        }

            public void CheckUpdate(object arg)
            {
                // This simulates my update process
                DownloadFile();
                Thread.Sleep(5000);
                Debug.WriteLine("Update completed");
            }
        
            public void DownloadFile()
            {
                var webClient = new WebClient();
        
                var progressBar = new ProgressBar(); 
                AddControl(progressBar);
        
                webClient.DownloadProgressChanged += (o, e) =>
                                                         {
                                                             Debug.WriteLine(e.ProgressPercentage);
                                                             progressBar.Value = e.ProgressPercentage;
                                                         };
                webClient.DownloadFileCompleted += (o, e) => Debug.WriteLine("Download completed.");
        
                Debug.WriteLine("Starting download...");
                webClient.DownloadFileAsync(new Uri("http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-netinst.iso"), "debian-504-i386-netinst.iso");
            }
        
            private void button1\_Click(object sender, EventArgs e)
            {
                ThreadPool.QueueUserWorkItem(CheckUpdate);
            }
        
            private void AddControl(Control control)
            {
                if (flowLayoutPanel1.InvokeRequired)
                    flowLayoutPanel1.BeginInvoke(new MethodInvoker(() => AddControl(control)));
                else
                    flowLayoutPanel1.Controls.Add(control);
            }
        
        }
        

        }

        This is also quite similar the code I'm actually using in my project. I forgot to mention that for this project I'm using NET 2.0 so maybe this can work differently in later versions.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups