Mubin
Mubin

Reputation: 4425

Terminate C# application properly and release all resources

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

Upvotes: 1

Views: 1355

Answers (2)

wigy
wigy

Reputation: 2222

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.

Upvotes: 2

LearningTechnology
LearningTechnology

Reputation: 1

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.

Upvotes: 0

Related Questions