none
WebClient class will continue downloads even after program termination RRS feed

  • General discussion

  • I am seriously puzzled by an odd behaviour of the WebClient class. I have created a very simple downloading program using WebClient.DownloadFileAsync(). The download will continue even after I cancel the download using WebClient.CancelAsync(), and will even continue after I terminate the program.

    This is only visible if you analyse the network traffic using a program like Fiddler (which I was using to debug my actual project), or if you look at your routers network traffic statistics.

    This would mean that the download thread is actually running in a separate process, not in the process of the application?!

    How to really cancel the download?

    I attach the code for your reference. I kept it as simple as possible.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net;
    
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            WebClient dl;
    
            public Form1()
            {
                InitializeComponent();
                dl = new WebClient();
                dl.DownloadFileCompleted += dl_DownloadFileCompleted;
                dl.DownloadProgressChanged += dl_DownloadProgressChanged;
    
            }
    
            void dl_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
            }
    
            private void dl_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
            {
                string termination = (e.Cancelled == true) ? " cancelled" : " finished";
                MessageBox.Show(e.UserState + termination);
            }
    
            void btDownload_Click(object sender, EventArgs e)
            {
                if (tbURL.Text != "")
                    dl.DownloadFileAsync(new Uri(tbURL.Text), tbFilename.Text, tbURL.Text);
            }
    
            private void btCancel_Click(object sender, EventArgs e)
            {
                dl.CancelAsync();
            }
        }
    }


    Friday, September 6, 2013 2:05 PM

All replies

  • <copied>

    This would mean that the download thread is actually running in a separate process, not in the process of the application?!

    <end>

    Threading is a process itself. The application that started the thread can terminate and leave the spawned thread running.

    So somehow, you have to find a way to kill the thread.

    Maybe,you need to raise a Complete event when you do the Cancel.

    http://msdn.microsoft.com/en-us/library/system.net.webclient.cancelasync.aspx

    Sunday, September 8, 2013 9:03 PM

  • Threading is a process itself. The application that started the thread can terminate and leave the spawned thread running.

    So somehow, you have to find a way to kill the thread.

    Threading is not "a process itself" and it's impossible for threads to run without a process on Windows since all process' threads are terminated when the process exits...

    I am seriously puzzled by an odd behaviour of the WebClient class. 

    This is only visible if you analyse the network traffic using a program like Fiddler (which I was using to debug my actual project), or if you look at your routers network traffic statistics.

    The problem is Fiddler, not your code or the WebClient. Fiddler being a "http debugger" acts as a proxy and does all HTTP requests and this includes all downloads - even if the program doing the download/http request has exited, fiddler keeps processing the http request (aka download) for your debugging pleasure in fiddler - so you need to cancel the download via fidder, do not kill your threads as suggested.

    Sunday, September 8, 2013 9:53 PM
  • @dmex

    <copied>

    Threading is not "a process itself" and it's impossible for threads to run without a process on Windows since all process' threads are terminated when the process exits...

    <end>

    I suggest that you create a Windows service, put some code in the Windows service to spawn up some threads, stop the Windows service without shutting those threads down, and you watch those threads continue to run with the code executing on those threads with the host Windows service not  being there with the service shutdown. :) 

    Sunday, September 8, 2013 10:12 PM
  • @demx

    I am going to try this, and if you are blowing smoke here, you are going to here about it. :)

    <copied>

    The problem is Fiddler, not your code or the WebClient. Fiddler being a "http debugger" acts as a proxy and does all HTTP requests and this includes all downloads - even if the program doing the download/http request has exited, fiddler keeps processing the http request (aka download) for your debugging pleasure in fiddler - so you need to cancel the download via fidder, do not kill your threads as suggested.

    <end>

    Sunday, September 8, 2013 10:16 PM
  • @darnold924

    Select the HTTP request in Fidder and you can see the download progress in the statusbar, you can right-click that connection and go Abort or press F3 to refresh the download progress ;)

    Sunday, September 8, 2013 11:05 PM