Results 1 to 11 of 11

Thread: [RESOLVED] Two timers running parallel to each other...

Threaded View

  1. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Two timers running parallel to each other...

    It is not clear what you are trying to do, but maybe this will help:

    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
    Create a new project with one button, copy / paste code. Run it and press the button.
    Last edited by dbasnett; Oct 28th, 2011 at 06:44 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width