Repetitive cycle in User inactivity

1

I need every 5 minutes to refresh a ListView that is loaded with a table, it works the first time but it does not work if there is still inactivity. I have tried several things without success.

Imports System.Runtime.InteropServices

Public Class Principal

    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 Principal_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Timer2.Start()

    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick

         Dim inactiveTime = GetInactiveTime()
         If (inactiveTime Is Nothing) Then
             Label5.Text = "Desconocido"
         ElseIf (inactiveTime.Value.TotalSeconds > 300) Then
             Label5.Text = String.Format("Inactivo por {0}segundos ",        
             inactiveTime.Value.TotalSeconds.ToString("#"))
             Limpiar_lista(Listview1)
             Mostrar_Lista(Listview1)
         Else
             Label5.Text = "Aplicacion Activa"
         End If

    End Sub

End Class   
    
asked by SandraP 12.09.2017 в 19:06
source

1 answer

1

Your problem is that after 300 seconds, you are no longer counting 300 seconds because your code, stays in place

ElseIf (inactiveTime.Value.TotalSeconds > 300) Then

that is always executed after 300 seconds.

What you could do is add a variable variable at the form level:

dim vuelta as int = 1;

and what you have to add is inside the if:

ElseIf (inactiveTime.Value.TotalSeconds > (300*vuelta )) Then
    Label5.Text = String.Format("Inactivo por {0}segundos ",        
    inactiveTime.Value.TotalSeconds.ToString("#"))
    Limpiar_lista(Listview1)
    Mostrar_Lista(Listview1)
    vuelta = vuelta +1
Else

and also:

 Else
    Label5.Text = "Aplicacion Activa"
    vuelta = 1
 End If

that variable is used to run every 300 seconds (when multiplying 300 by the turn is every 5 minutes), and when the application becomes active again, we reset it.

    
answered by 12.09.2017 в 23:50