run a code every 5 seconds in vb.net

0

I have a code that checks if the application is running, if it is not, the code starts application, if it is and it passes ... But it does so only that I execute it for the 1st time. I wish he checked every 5 seconds .. * in vb.net

code:

Sub Main()
    Dim activo As Boolean
    Dim myprocesses As Process()
    myprocesses = Process.GetProcessesByName("check") 
    If myprocesses.Length > 0 Then  
        activo = True 
    Else
        activo = False 
        Dim p As New ProcessStartInfo("check.exe")
        p.WindowStyle = ProcessWindowStyle.Hidden
        p.CreateNoWindow = True
        Process.Start(p)

    End If

    System.Console.ReadKey()

End Sub
    
asked by anonymous 27.02.2017 / 22:58

1 answer

0

You can use the Timer class:

'Declare um variável do tipo Timer: 
Private tempo As New System.Timers.Timer(5000) '5000 = 5 segundos

'Adicione um handler para capturar o evento tick do timer: 
AddHandler tempo.Elapsed, AddressOf DispararTimer

'Adicione a sub que representa o evento tick do timer:
Public Sub DispararTimer(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
  'Código que você quer que execute de 5 em 5 segundos
End Sub

'Por último, no load do formulário, habilite o timer:
tempo.Enabled = True

Answer based on in this Macoratti post .

    
28.02.2017 / 14:31