Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winform freezes when I try to close it

Tags:

c#

.net

winforms

I have following code

private void btnClose_Click(object sender, System.EventArgs e)
{
     Close();
} // btnClose_Click

After running the close winforms function the application freezes. Any idea why it can happen?

I use .net 2.0 and i run under Windows 7 vs2005

EDIT:

After I pressed pause in the debugger I came to

private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  // GUI
  if (FScannerThread_Running)
  {
    FScannerThread_Running = false;
    FScannerThread.Join();
  }
}

And it stacked in FScannerThread.Join(); any idea how i can kill it ?

like image 951
Night Walker Avatar asked Dec 19 '25 17:12

Night Walker


2 Answers

Use the bool Thread.Join(int milliseconds) overload, and if the result is false, Abort() your thread.

private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    // GUI
    if (FScannerThread_Running)
    {
        FScannerThread_Running = false;
        if (!FScannerThread.Join(1000)) // Give the thread 1 sec to stop
        {
            FScanner.Abort();
        }    
    }
}

Note that you should catch ThreadAbortException in your thread in order to end it gracefully (if you have something to release for instance).

like image 165
Shimrod Avatar answered Dec 21 '25 06:12

Shimrod


When it is freezed, call pause (Break all) in the debug menu - you'll see where diferent threads of your app could hang up. The platform here does not matter. Sometimes that can happen if you have multithreaded app and in some thread the state was corrupted or something like that.

like image 21
Sasha Reminnyi Avatar answered Dec 21 '25 07:12

Sasha Reminnyi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!