Timer in threading

This code sample creates a time and starts it in a new thread. To disable the thread we use the same timer as shown in the example.



'To create the timer for the threading part
Public KeepAliveDelegate As Threading.TimerCallback
Public KeepAliveTimer As System.Threading.Timer
'end

KeepAliveDelegate = AddressOf KeepAliveTimerHandler

'To start the threading timer.
KeepAliveTimer = New System.Threading.Timer(KeepAliveDelegate, Nothing, 100, 100)
KeepAliveTimer.Change(100, 100)

Private Sub KeepAliveTimerHandler(ByVal state As Object)
'Add code to run until the thread is stoped
End sub

'To stop the thread or to disable the thread
KeepAliveTimer.Change(0, System.Threading.Timeout.Infinite)

'to restrat the thread again using the below code
KeepAliveTimer = New System.Threading.Timer(KeepAliveDelegate, Nothing, 0, 100)


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: