Determine how the application closes

I am trying to determine if my application is closed by clicking the β€œX” in the Windows form or by clicking on the β€œExit” button that I have. Right now I am using StackTrace.GetFrame (someIndex) to determine how, but I am looking for a more definitive way, since it looks like these HR orders arent guaranteed. Is there a better way to make a difference? These are .NET 3.5 WinForm and Im written in C #.

+3
source share
2 answers

Use another event to access your own Exit button. In your own Exit event handler, execute additional logic or set some state variable and then call the method of closing a regular application.

Send some examples of how your events are connected, and I will give a more specific example. In general, it would look something like this:

private void btnMyExit_Click(object sender, EventArgs e)
{
    // TODO: add any special logic you want to execute when they click your own "Exit" button
    doCustomExitWork();
}

public static void OnAppExit(object sender, EventArgs e)
{
    doCustomExitWork();
}

private void doCustomExitWork()
{
    // TODO: add any logic you want to always do when exiting the app, omit this whole method if you don't need it
}
+4
source

Use the FormClosing event and query FormClosingEventArgs for the CloseReason value to enumerate.

0
source

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


All Articles