Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer tick handler not running

Tags:

vb.net

timer

I'm completely new to VB.net, and I'm trying to run a semi-intermediate script that checks for certain files being open. When it first opens, it checks for one particular program, then it will continue checking for a different program on a timer.. However; when I run the code, the Sub Timer1 never runs, I have it set to run every 20 seconds..

Imports System.Net
Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Process.GetProcessesByName("PROGRAM1").Length >= 1) Then
            MessageBox.Show("This Client is already running!", "IG Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Environment.Exit(0)
        Else
            Process.Start(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "program.exe"))
            '''' OPEN PROGRAM ABOVE ''''
        End If
        For Each frm As Form In Application.OpenForms
            frm.WindowState = FormWindowState.Minimized
        Next frm
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (Process.GetProcessesByName("PROGRAM2").Length >= 1) Then 'CHECK FOR PROGRAM
            MessageBox.Show("Program is running!", "IG Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Environment.Exit(0)
            Form3.Show()
        Else
            MessageBox.Show("Program is not running!")
        End If
    End Sub
End Class

Above is the code I already have.. my timer sub either isn't running or isn't checking every 20 seconds. Any ideas?

like image 424
Jordan ChillMcgee Ludgate Avatar asked Oct 22 '22 16:10

Jordan ChillMcgee Ludgate


1 Answers

Common mistakes with timers are:

  1. You need to start it with either timer.enabled or timer.start.

  2. You might need to reset the timer in the tick handler, depending on the type of timer and property settings. (There is the timer control, system.timers.timer, and system.threading.timer, each of which is a little different.)

  3. You might need to disable temporarily it in the tick handler to make sure it doesn't re-enter the handler and cause problems.

  4. If you need to wait while a timer is running, it is MUCH better to use system.threading.thread.sleep rather than a loop.

like image 89
xpda Avatar answered Oct 28 '22 15:10

xpda