CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: app auto close

  1. #1
    Join Date
    Sep 2001
    Posts
    254

    app auto close

    greeting,

    how EXE can close by itself , if no activity on a certain period of time.

    thnks
    cyrus

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: app auto close

    To close it, obviously use Unload Me for each form, but to test for inactivity, that is where the fun comes in.

    You must have a Timer on your form, testing a Boolean variable. Then, for each control on your form, you have to set that variable to True / False whenever any event happens. Here is a very basic example :

    Code:
    Option Explicit
    
    Private blnActive As Boolean
    
    Private Sub Command1_Click()
    blnActive = True
    End Sub
    
    Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
    blnActive = True
    
    End Sub
    
    Private Sub Command1_KeyPress(KeyAscii As Integer)
    blnActive = True
    
    End Sub
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    blnActive = True
    
    End Sub
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    blnActive = True
    
    End Sub
    
    Private Sub Form_Click()
    blnActive = True
    
    End Sub
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    blnActive = True
    
    End Sub
    
    
    Private Sub Form_KeyPress(KeyAscii As Integer)
    blnActive = True
    
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    blnActive = True
    
    End Sub
    
    Private Sub Text1_Change()
    blnActive = True
    
    End Sub
    
    Private Sub Text1_Click()
    blnActive = True
    
    End Sub
    
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    blnActive = True
    
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    blnActive = True
    
    End Sub
    
    Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    blnActive = True
    
    End Sub
    
    Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    blnActive = True
    
    End Sub
    
    Private Sub Timer1_Timer()
    Static intCount As Integer
    
    If Not blnActive Then
        intCount = intCount + 1
        Label1.Caption = intCount
        
            If intCount >= 20 Then
                Unload Me
            End If
    Else
        intCount = 0
        Label1.Caption = intCount
    End If
    
    
    End Sub
    It may not be 100% correct, but it should give you a good start.

  3. #3
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: app auto close

    In addition to HanneSThEGreaT, you can reference How to Make a Program close itself if it has been inactive for long time also.
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

  4. #4
    Join Date
    Oct 2006
    Posts
    327

    Re: app auto close

    Hello,

    Would it really be a bad idea using the GetInputState function of user32 ?

    It :
    determines whether there are mouse-button or keyboard messages in the calling thread's message queue.
    It needs then a timer and a static variable (to retrieve the time T1 each time there is an activity). You decide to close if The Time T2 - Time T1 >= accepted delay.

    You would that way save to attach code to every control ...

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: app auto close

    Not bad, but seems tricky, to me.
    You'd have to interrogate GetInputState() very often because messages are consumed very fast, so you might miss the time, them being in the message queue, thinking there was no activity and closing.

    I suppose you might have to subclass the window, so as every message is running over your own WindowProc where you can always reset the timeout-counter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured