Click here to Skip to main content
Licence 
First Posted 12 Oct 2002
Views 86,793
Bookmarked 25 times

Find out what's closing your application

By MrPolite | 15 Oct 2002
Code in VB.NET to find out what's closing your application.
1 vote, 20.0%
1

2

3
1 vote, 20.0%
4
3 votes, 60.0%
5
2.64/5 - 15 votes
μ 2.64, σa 3.03 [?]

Introduction

You can use this code to see why your application is closing (closed by user, by Windows, by code?). It can be very handy if you are using a NotifyIcon in your app. You can figure out if the user is closing your app or not, if so, then you can just minimize it to tray.

For those who have used Visual Basic before, you know that there was a QueryUnload method for your Forms and that you could find out the reason for the form's closing by examining the UnloadMode variable.

This code snippet shows you how to do this in .NET. The sample code is in VB.NET. The code is from GotDotNet message boards, written by "YeahIGotDotNet".

Private Sub Form1_Closing(ByVal sender As Object, _
  ByVal e As System.ComponentModel.CancelEventArgs) _
  Handles MyBase.Closing
    Dim O As System.Diagnostics.StackTrace = _
           New System.Diagnostics.StackTrace(True)
    Dim F As System.Diagnostics.StackFrame

    F = O.GetFrame(7)

    Select Case F.GetMethod.Name.ToString
        Case "SendMessage"
            MsgBox("Closing because of call in code.")
        Case "CallWindowProc"
            MsgBox("Closing because of system menu click.")
        Case "DispatchMessageW"
            MsgBox("Closing because of Task Manager.")
        Case Else
            MsgBox("Don't Know why I'm closing!!??")
    End Select
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MrPolite



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralHandles Mybase.closing Pinmemberatogwe9:04 11 Jun '07  
GeneralUsing WM_QUERYENDSESSION to detect Windows shutdown Pinmemberrodgerc9:13 2 Oct '06  
GeneralOverriding WndProc changes the GetMethod.Name properties PinmemberHanreG3:15 7 Nov '03  
GeneralCoversion of C++ method PinmemberBogBrush Zone5:12 31 Aug '03  
I have recently converted an application that does this from VC++/MFC to C#.   The following is a conversion of the method I used in that application by overriding OnSysCommand and essentially handling the event when it is a button press on the Close button/menu item and before it is translated into a request to close the window.
 
private const int WM_SYSCOMMAND = 0x0112;     // From winuser.h
private const int SC_CLOSE = 0xF060;
 
protected override void WndProc(ref Message m)
{
     if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
     {
          // Perform actions
     }
     else
          base.WndProc (ref m);
}
 
N.B. I have also posted this to GotDotNet Wink | ;)
 
Gerard Krupa BSc AMBCS
GeneralMore complete implementation PinmemberEvilDoctorSmith9:23 28 Oct '02  
GeneralClosing a User Control PinsussPeter S7:06 16 Oct '02  
GeneralRe: Closing a User Control PinmemberDavid Stone15:55 16 Oct '02  
GeneralRe: Closing a User Control PinmemberBarryJ21:09 16 Oct '02  
GeneralRepost PinmemberRichard_D0:05 14 Oct '02  
GeneralRe: Repost PinsussAnonymous15:13 14 Oct '02  
GeneralRe: Repost PinmemberRichard_D23:40 14 Oct '02  
GeneralRe: Repost PinsussAnonymous19:36 14 Oct '02  
GeneralRe: Repost PinmemberRichard_D23:47 14 Oct '02  
GeneralRe: Repost PinmemberKourosh13:22 16 Oct '02  
GeneralRe: Repost PinmemberRichard_D0:02 17 Oct '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 16 Oct 2002
Article Copyright 2002 by MrPolite
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid