Revuen Ben Dror
Revuen Ben Dror

Reputation: 237

Why i'm getting a win32exception error when exiting my application?

On Form1 closing event sometimes i'm getting the exception when select YES to close the application on the line :

Environment.Exit(0);

This is the Form1 closing event code :

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {    
            if (MessageBox.Show("Are you Sure you want to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
                Environment.Exit(0);
            }


        }

The exception : Error creating window handle

This is the complete exception error message :

System.ComponentModel.Win32Exception was unhandled
  HResult=-2147467259
  Message=Error creating window handle.
  Source=System.Windows.Forms
  ErrorCode=-2147467259
  NativeErrorCode=1406
  StackTrace:
       at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       at System.Windows.Forms.Control.CreateHandle()
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.VisualStyles.VisualStyleRenderer.DrawParentBackground(IDeviceContext dc, Rectangle bounds, Control childControl)
       at System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds, Boolean up)
       at System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintWorker(PaintEventArgs e, Boolean up, CheckState state)
       at System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintUp(PaintEventArgs e, CheckState state)
       at System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.Paint(PaintEventArgs pevent)
       at System.Windows.Forms.ButtonBase.OnPaint(PaintEventArgs pevent)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 

Maybe i need to release/close/dispose something which i didn't ?

Sometimes the exception is show up but sometimes not now i tried to close the application like 5 times in a row without any exception .

Upvotes: 1

Views: 3400

Answers (1)

Tieson T.
Tieson T.

Reputation: 21246

You don't really need an else condition - if they select 'no' you simply allow the form to close. The only obvious reason I can see for calling Environment.Exit(0) is when you have non-modal forms (or other processes) you also want/need to close. It might be better to cycle through the child forms and trigger their close events. I used something like this at one point:

// Close each child window of this form
foreach ( Window window in Application.Current.Windows )
{
    if ( window != null && window.Owner == this )
    {
        window.Close();
    }
}

It's been awhile, but I believe you have to manually assign the Owner property to a form when creating it.

Of course, you may have other, perfectly valid reasons to call Environment.Exit(0).

Upvotes: 3

Related Questions