Console APP with Timer not running

岁酱吖の 提交于 2021-01-28 11:02:31

问题


I did this first into a WinForm project, now I've changed the application type to "Console application", I've deleted the form1.vb, changed the startup object to this "Module1.vb" but now I can't run the app. well the app runs but the timer tick is doing nothing, the code is exactly the same, I only did one change for the sub main/form1_load name

What I'm doing wrong?

PS: I've tested if the error was in the conditional of the lock method and all is good there, the problem is with the ticker event but I don't know why.

#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

UPDATE

I made these changes like in the examples of 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

But the tick/elapsed is still doing nothing...


回答1:


FROM MSDN

Windows.Forms.Timer

Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.

You need a System.Timer
Of course this requires a different event Handling (Example taken from 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 



回答2:


You could use Windows.Forms.Timer in console application if you add Application.Run() at the end of your main().

This kind of timer might be useful in some console applications if you are using any offscreen Windows.Forms object - ie.: for offscreen rendering - these objects can't be simply accessed from System.Timer since it fires on separate thread (than the one where Windows.Forms object was created on).

Otherwise by all means use the System.Timers.Timer or System.Threading.Timer



来源:https://stackoverflow.com/questions/15761022/console-app-with-timer-not-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!