1

我首先在 WinForm 项目中执行此操作,现在我已将应用程序类型更改为“控制台应用程序”,删除了 form1.vb,将启动对象更改为此“Module1.vb”但现在我无法运行应用程序。应用程序运行良好,但计时器滴答未做任何事情,代码完全相同,我只对sub main/form1_load名称进行了一次更改

我做错了什么?

PS:我已经测试了错误是否在 lock 方法的条件下并且一切都很好,问题出在 ticker 事件上,但我不知道为什么。

#Region " Vars "

    Dim Running As Boolean = False
    Dim Errors As Boolean = False

    Dim Executable_Name As String = Nothing

    Dim Toogle_Key As System.Windows.Forms.Keys = Nothing
    Dim WithEvents Toogle_Key_Global As Shortcut = Nothing

    Dim Executable_Timer As New Timer
    Dim Lock_Timer As New Timer
    Dim Lock_Interval As Int32 = 10
    Dim Lock_Sleep As Int32 = Get_Milliseconds(3)

    Dim Screen_Center_X As Int16 = (Screen.PrimaryScreen.Bounds.Width / 2)
    Dim Screen_Center_Y As Int16 = (Screen.PrimaryScreen.Bounds.Height / 2)

#End Region

    ' Load
Sub main()
    Pass_Args()
    Sleep()
    Lock()
End Sub

    ' Lock
Private Sub Lock()
    If Process_Is_Running(Executable_Name) Then
        AddHandler Lock_Timer.Tick, AddressOf Lock_Tick
        AddHandler Executable_Timer.Tick, AddressOf Executable_Tick
        Lock_Timer.Interval = Lock_Interval
        Lock_Timer.Start()
        Executable_Timer.Start()
        Running = True
    Else
        Terminate()
    End If
End Sub

' Lock Tick
Private Sub Lock_Tick()
    Console.WriteLine("test")
    If Running Then Cursor.Position = New Point(Screen_Center_X, Screen_Center_Y)
End Sub

更新

我在 MSDN 的示例中进行了这些更改:

Dim Executable_Timer As New System.Timers.Timer
Dim Lock_Timer As New System.Timers.Timer

AddHandler Lock_Timer.Elapsed, AddressOf Lock_Tick
AddHandler Executable_Timer.Elapsed, AddressOf Executable_Tick

但是滴答声/已逝去的仍然没有做任何事情......

4

2 回答 2

3

来自 MSDN

Windows.Forms.Timer

实现一个以用户定义的时间间隔引发事件的计时器。此计时器针对在 Windows 窗体应用程序中的使用进行了优化,并且必须在窗口中使用。

您需要一个System.Timer
当然这需要不同的事件处理(示例取自 MSDN)

    ' Create a timer with a ten second interval.
    Dim aTimer = new System.Timers.Timer(10000)

    ' Hook up the Elapsed event for the timer.
    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

    ....

    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
         Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
    End Sub 
于 2013-04-02T09:54:02.203 回答
1

Windows.Forms.Timer如果您Application.Run()main().

如果您使用任何屏幕外对象,这种计时器在某些控制台应用程序中可能很有用Windows.Forms- 即:对于屏幕外渲染 - 这些对象不能简单地访问,System.Timer因为它在单独的线程上触发(而不是在Windows.Forms创建对象的线程上) )。

否则一定要使用System.Timers.TimerorSystem.Threading.Timer

于 2013-12-31T14:08:32.627 回答