Hola, esto se puede hacer obteniendo lo que se llama "
user idle time", que seria el tiempo inactivo, se hace con una api llamada
GetLastInputInfo, aca te paso un ejemplo simple de como detectar si estuvo 5 segundos inactiva, adaptalo a tus necesidades y lo unico que tendrias que hacer es cerrar la aplicacion cuando se cumpla esa condicion.
Código vb:
Ver originalImports System.Runtime.InteropServices
Public Class Form1
Private Structure LASTINPUTINFO
Public cbSize As UInteger
Public dwTime As UInteger
End Structure
<DllImport("User32.dll")> _
Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
End Function
Public Function GetInactiveTime() As Nullable(Of TimeSpan)
Dim info As LASTINPUTINFO = New LASTINPUTINFO
info.cbSize = CUInt(Marshal.SizeOf(info))
If (GetLastInputInfo(info)) Then
Return TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime)
Else
Return Nothing
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim inactiveTime = GetInactiveTime()
If (inactiveTime Is Nothing) Then
Me.Text = "Desconocido"
Me.BackColor = Color.Yellow
ElseIf (inactiveTime.Value.TotalSeconds > 5) Then
Me.Text = String.Format("Inactivo por {0}segundos", inactiveTime.Value.TotalSeconds.ToString("#"))
Me.BackColor = Color.Red
Else
Me.Text = "Aplicacion Activa"
Me.BackColor = Color.Green
End If
End Sub
End Class
solo tenes que poner un control
Timer() en el formulario.
saludos.