1

I'm working on a books downloading application, it works fine, but if clicked on Exit button before download complete, the program throws an exception, I handled that exception, but after that, when I press Exit button, program get closed, but it doesn't terminates completely, it continue to execute unless killed from Task Manager.

Here is snippet.

try
{            

    string placeholder = " KB";
    string placeholder1 = " KB";
    double bytesIn = double.Parse(e.BytesReceived.ToString());
    double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
    // get the elapsed time in milliseconds
    ctime = stopwatch.ElapsedMilliseconds;
    // get the received bytes at the particular instant
    current = e.BytesReceived;
    // calculate the speed the bytes were downloaded and assign it to a Textlabel (speedLabel in this instance)
    string speed = ((int)(((current - previous) / (double)1024) / ((ctime - ptime) / (double)1000))).ToString() + " KB/s";
    previous = current;
    ptime = ctime;
    double percentage = bytesIn / totalBytes * 100;
    bytesIn = Math.Round(bytesIn / 1024, 2);
    if (bytesIn > 2000)
    {
        bytesIn = Math.Round(bytesIn / 1024, 2);
        placeholder = " MB";
    }
    totalBytes = Math.Round(totalBytes / 1024, 2);
    if (totalBytes > 2000)
    {
        totalBytes = Math.Round(totalBytes / 1024, 2);
        placeholder1 = " MB";
    }
    this.BeginInvoke((MethodInvoker)delegate
    {
        labelPercentage.Text = "Downloading " + Convert.ToInt32(percentage) + "%  -  " + bytesIn + placeholder + " / " + totalBytes + placeholder1 + " @ "+ speed;
        downloadProgressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
    });
}
catch (Exception)
{
    AutoClosingMessageBox.Show("Terminating..", "Closing", 2000);
    this.Close();
    System.Windows.Forms.Application.Exit();
}

PS: it does displays the message box before terminating and main thread killed but I guess some other thread keep alive in background

Edit

Now I'm invoking last two lines only, and I get InvalidOperationException, when I don't catch it, and it exception occurs in invoke

This is where all this being called.

client.DownloadProgressChanged += client_DownloadProgressChanged;

When I Press Exit button, program get terminated, but it continue to execute according to VS.

I'm using VS 13 Pro

wigy
  • 2,174
  • 19
  • 32
Mubin
  • 4,325
  • 5
  • 33
  • 55
  • Possible duplicate of [How to close all running threads?](http://stackoverflow.com/questions/2688923/how-to-close-all-running-threads) – Gabriel Sadaka Nov 02 '15 at 02:48
  • @gabriel: thanks, but I guess if this could be threads issue, then it shouldn't throw exception. – Mubin Nov 02 '15 at 02:52
  • 2
    What exception gets thrown? Where does it throw at? As a side note, most of what you are doing doesn't need to be invoked on the UI thread, just the last two lines need to be invoked on the UI thread. Is this being run in a background worker? What calls this method to update the download? When running in the debugger, and pressing "Pause" after closing the second time, what line does it highlight? – Ron Beyer Nov 02 '15 at 03:25
  • @RonBeyer: Updated my question. – Mubin Nov 02 '15 at 11:11

2 Answers2

2

Based on the sources, I assume you are using the WebClient component. Before exiting the application, you need to cancel downloading. I would just add a client.Dispose() call to the Closed event handler on the window that contains the WebClient.

Edit

Additionally, if this doesn't work, put Environment.Exit(0); and this will terminate as expected.

Mubin
  • 4,325
  • 5
  • 33
  • 55
wigy
  • 2,174
  • 19
  • 32
  • I've added that on `Exit button click event`, this `Dispose` the client first and then terminating application, but still the same issue. – Mubin Nov 02 '15 at 17:48
0

Try to use the "using" keyword and put your code inside the 'using' block. That should terminate your application properly as you expect by disposing all the variables.