vb.net 计时器滴答处理程序未运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15057542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:30:22  来源:igfitidea点击:

Timer tick handler not running

vb.nettimer

提问by Jordan ChillMcgee Ludgate

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..

我是 VB.net 的新手,我正在尝试运行一个半中间脚本来检查是否打开了某些文件。当它第一次打开时,它会检查一个特定的程序,然后它会继续在计时器上检查不同的程序。当我运行代码时,Sub Timer1 从不运行,我将它设置为每 20 秒运行一次。

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?

以上是我已经拥有的代码..我的计时器子要么没有运行,要么没有每 20 秒检查一次。有任何想法吗?

回答by xpda

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.

  1. 您需要使用 timer.enabled 或 timer.start 启动它。

  2. 您可能需要在滴答处理程序中重置计时器,具体取决于计时器的类型和属性设置。(有定时器控件,system.timers.timer和system.threading.timer,每一个都有点不同。)

  3. 您可能需要在滴答处理程序中暂时禁用它,以确保它不会重新进入处理程序并导致问题。

  4. 如果您需要在计时器运行时等待,最好使用 system.threading.thread.sleep 而不是循环。