execute a code every 5 seconds on Vb.net

Asked

Viewed 1,003 times

2

I have a code that checks if the application is running, if it is not running, the code starts the application, if it is and it passes... But he does it just qnd I run for the 1° time. I qria q he checked every 5 seconds.. *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
  • Could you explain better what’s going on? Show what you’re trying to do? That is, show your code to help people understand what you want.

  • I edited, like this, I qro that every 5 seconds the code I edited up there runs. 1-I STARTED THE APPLICATION, 2-CHECKED, 3-PASSED 5 SECOND, 4-CHECKED... and so on

1 answer

0

You can use the class Timer:

'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

Response based in this post from Macoratti.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.