开发者

Determine how an application is closed

I am trying to determine if my application is closed through clicking the "X" on the windows form, or if they clicked an "Exit" button I have on it. 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 frame orders arent guaranteed. Is there a better way to make the distinction? This is a .NET 3.5 WinForm, and Im wri开发者_JS百科ting in C#.


Use a different event to handle your own "Exit" button click. In your own "Exit" event handler do your extra logic, or set some state variable, and then call the normal application close method.

Post some samples of how your events are wired up and I get 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
}


Use the FormClosing event and query the FormClosingEventArgs for the enum CloseReason value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜