Application Auto Shutdown

42 Views Asked by At

I am building a simple application for a specific group of users that are particularly busy. The application access a license; this is the only license. If one of the persons running the app, forgets the app open, others cannot access the resource. I'm thinking using a timer to set up a timeout for the app so that is the specific person has not completed the interaction with the License, the application will properly shutdown so that the license is released. The app is a Windows forms in vb.net. How can I set a timer to let's say 15 minutes and perform the shutdown if it expires or dismiss it if the operator concludes its job within the time limit? All samples of how to are appreciated.

1

There are 1 best solutions below

1
Roberto Hernandez On
Imports System.Timers
Public Class Form1
    Private Timer As New Timer
    Private LastActivity As DateTime = Now()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer.Interval = 60000  'in milisecons = 1 minute
        AddHandler Timer.Elapsed, New ElapsedEventHandler(AddressOf TimerElapsed)
        AddHandler Application.Idle, AddressOf Application_Idle
        Timer.Start()
    End Sub
    Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
        LastActivity = Now()
        Label1.Text = LastActivity 'Whit this you can see the last user ativiti, you can remove this line.
    End Sub
    Private Sub TimerElapsed(sender As Object, e As ElapsedEventArgs)
        If DateDiff(DateInterval.Minute, LastActivity, Now) >= 15 Then Application.Exit() 'force the end of the application
    End Sub
    Private Sub Form1_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
        RemoveHandler Application.Idle, AddressOf Application_Idle
    End Sub
End Class