Prevent Windows from closing an application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kevininstructor@state.or.us

    Prevent Windows from closing an application

    I want to prevent a user from closing an application while doing critical
    operations. The following code (concept came from MSDN) works except for
    when the user attempts to terminate via "Task List" which causes "Program
    not responding"...p ress "Cancel" and no issues, press "End now" and sure
    enough it ends.

    My question is; is there any method to prevent the "Applicatio n not
    responding" message from appearing?

    Current code:
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~
    #Region " Windows Form Designer generated code "
    Private bWasClick As Boolean
    Private bAllowExit As Boolean
    ....
    Public Const SC_CLOSE As System.Int32 = 61536
    Public Const WM_SYSCOMMAND As System.Int32 = 274


    Protected Overloads Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt3 2 = SC_CLOSE Then
    Me.bWasClick = True
    If Me.CheckBox1.Ch ecked Then
    Return
    End If
    End If
    MyBase.WndProc( m)
    End Sub

    ....

    Private Sub CheckBox1_Check edChanged(ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles CheckBox1.Check edChanged
    bAllowExit = DirectCast(send er, CheckBox).Check ed()
    End Sub

    Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
    System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
    If Me.bWasClick Then
    '...
    Else
    '...
    End If
    e.Cancel = bAllowExit
    End Sub

    Visual Studio 1.1

    Thanks for any assistance and for taking the time to read this question!
    Kevin


  • Cor Ligthert

    #2
    Re: Prevent Windows from closing an application

    Kevin,

    When you simple use the e.cancel in the window closing event is it much
    easier

    (It is nice to show a messagebox before you set this to true) otherwise it
    can be called a kind of bug.

    Cor


    Comment

    • kevininstructor@state.or.us

      #3
      Re: Prevent Windows from closing an application

      I left the messagebox out for clarity...at this point it ask to proceede or
      abort rather then be rude to simply say no.

      "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
      news:uY0OkkDbFH A.3016@tk2msftn gp13.phx.gbl...[color=blue]
      > Kevin,
      >
      > When you simple use the e.cancel in the window closing event is it much
      > easier
      >
      > (It is nice to show a messagebox before you set this to true) otherwise it
      > can be called a kind of bug.
      >
      > Cor
      >[/color]


      Comment

      Working...