Search code examples
c#winapic#-4.0c#-3.0pinvoke

How to detect differentiate external wm_close vs internally triggered by form.close()


This is a C# app that sits in the tray as a notifyicon and does its stuff until someone right clicks it and selects close (menu option) or it gets a wm_close from an external app or by the operating system say during a reboot.

protected override void WndProc(ref Message m)
{
   case  Win32.WmClose:
  //recvd message to shutdown
   Program.Log.InfoFormat("Shutdown received at {0}", DateTime.Now);
   CleanUp();
   this.Close(); //this is the main form
   break;

   //other case statements here
}

//somewhere else on menu exit of notify icon
 private void toolStripMenuItemExit_Click(object sender, EventArgs e)
 {
        Program.Log.InfoFormat("Manual EXIT at {0}", DateTime.Now);
        CleanUp();
        this.Close(); //this is the main form
 }

The this.close() triggers another WM_CLOSE sending the app in a tailspin. What is the correct way to handle this situation ? thank you


Solution

  • Handle form Closing event. and whenever you want to exit just call Close();, and make any other operation rely on closing inside the closing event instead of handling it at WndProc and toolStripMenuItemExit_Click, so:

    private void OnFormCloseing(object sender, FormClosingEventArgs e)
    {
        string reason = string.Empty;
        switch (e.CloseReason)
        {
            case CloseReason.UserClosing:
                reason = "Manual EXIT";
                break;
    
            case CloseReason.WindowsShutDown:
                reason = "Shutdown received";
                break;
        }
        Program.Log.InfoFormat(reason + " at {0}", DateTime.Now);
        CleanUp();
    }
    
    private void toolStripMenuItemExit_Click(object sender, EventArgs e)
    {
        this.Close(); //this is the main form
    }
    

    More members of CloseReason here.