It is not clear what you are trying to do, but maybe this will help:
Create a new project with one button, copy / paste code. Run it and press the button.Code:Public Class Form1 'a timer that fires periodically Dim WithEvents aTimer As New System.Threading.Timer(AddressOf TickTock, Nothing, 0, 500) 'a stopwatch for each event Dim swEV1 As New Stopwatch Dim swEV2 As New Stopwatch 'how long between executions Dim ev1Time As New TimeSpan(0, 0, 1) Dim ev2Time As New TimeSpan(0, 0, 5) 'test Private Sub Button1_Click(sender As System.Object, _ e As System.EventArgs) Handles Button1.Click Button1.Enabled = False 'start the test swEV1.Start() swEV2.Start() End Sub Dim ev1 As New Threading.Thread(AddressOf event1) Dim ev2 As New Threading.Thread(AddressOf event2) Private Sub TickTock(state As Object) If swEV1.IsRunning Then 'check the elapsed time and run the thread when needed 'only one thread per event is allowed to run If swEV1.Elapsed >= ev1Time Then If Not ev1.IsAlive Then swEV1.Reset() 'reset the stopwatch swEV1.Start() ev1 = New Threading.Thread(AddressOf event1) ev1.IsBackground = True ev1.Start() End If End If If swEV2.Elapsed >= ev2Time Then If Not ev2.IsAlive Then swEV2.Reset() swEV2.Start() ev2 = New Threading.Thread(AddressOf event2) ev2.IsBackground = True ev2.Start() End If End If End If End Sub Private Sub event1() Debug.WriteLine("EV1 " & DateTime.Now.ToString) Threading.Thread.Sleep(500) 'simulate work End Sub Private Sub event2() Debug.WriteLine("EV2 " & DateTime.Now.ToString) Threading.Thread.Sleep(3000) 'simulate work End Sub End Class




Reply With Quote
