Why does the .NET application prevent Windows from shutting down?

One of my applications prevents windows from closing if it is running.

The only place I suspect the reason could be the FormClosing event handler, which, however, is pretty standard:

EDIT: Removing this handler does not make a difference at all, so the reason is somewhere else.

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason != CloseReason.UserClosing)
    {
        StopAllThreads(); 
        //let close
        return;
    }
    //ask user whether he wants to save his work
}

I could not reproduce this with the simplest possible application containing only this FormClosing handler - a simple application closes correctly when windows start to close.

What else can prevent windows from closing? Where should I look for code to debug this problem?

I do not have a special implementation of WndProc in my main form. This is a .NET 2.0 application.

, " Windows " (Windows 7) . Windows Visual Studio, , .


EDIT: StopAllThreads

public static void StopAllThreads()
{
    lock (syncLock)
    {
        foreach (IStop stoppable in stoppables)
        {
            try
            {
                stoppable.Stop(); //stops a running thread by setting a volatile boolean flag
            }
            catch (Exception ex)
            {
                Debug.Fail("Error stopping a stoppable instance: " + ex.ToString());
            }
        }
        stoppables.Clear();
        disposed = true;
    }
}

: , .

+3
3

: OnClosing, :

    protected override void OnClosing(CancelEventArgs e)
    {
        e.Cancel = true;
        this.Hide();
    }
+3

, , - ? , - .

+1

e.Cancel. , , form1 form2. , form1, , "X" .

Class form1
    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim oClosingEvent As System.Windows.Forms.FormClosingEventArgs
    oClosingEvent = e
    If oClosingEvent.CloseReason = CloseReason.WindowsShutDown Then
        'condition1
        'Operating system requested the form to be closed
        ElseIf oClosingEvent.CloseReason = CloseReason.UserClosing Then
        'condition2
        'User has clicked the X symbol on form title
        'or another piece of code such as from1.close is executed
        e.Cancel = True
        Me.Hide()
    Else
    'condition3
    'Unknown reason
    End If
    End Sub
End class

, from2.

Class form2
Private Sub Me_Closing() Handles Me.Closing
    form1.close
End Sub
End class

, 2. form2 form1. form1 , form2, form1.

, form1.close form2.

Class form2
Private Sub Me_Closing() Handles Me.Closing
' the following line must be removed.    
'form1.close
End Sub
End class

During window closing, if you call the code to close form 1, closing the form 1 will be prevented, and the Application will not allow Microsoft Windows to close the application.

0
source

Source: https://habr.com/ru/post/1761268/


All Articles