0

I have two System.Windows.Forms.Timer with different interval having code as below

 Private Sub myTimer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer1.Tick
    myTimer1.Enabled = False
    'Code
    myTimer1.Enabled = True
End Sub

 Private Sub myTimer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer2.Tick
    myTimer2.Enabled = False
    'code
    myTimer2.Enabled = True
End Sub

but some timer2 not start wroking until timer1 not get finished i want them to run parallel without waiting for each other to finish

samirprogrammer
  • 438
  • 5
  • 18

2 Answers2

0

Using a Timer and Thread.Sleep is not a good idea as Thread.Sleeps blocks the execution of any code inside the current thread. Thus, the other timer can't execute.

Use the timer for the "waiting" and leave out the Thread.Sleep.

Matthias
  • 12,053
  • 4
  • 49
  • 91
0

From your problem I suppose you are using System.Windows.Forms.Timer which is designed for a single-threaded environment. I suspect that when you enter in the first event it block execution until you exit from the call (And the Sleep() inside is really a bad choiche).
System.Timers.Timer is designed for multithread execution. This means that to call your event, Window use a different Thread without blocking your main thread. Of course this led to the famous problem on the update of user controls from a thread not the UI thread.

This article on MSDN Magazine explains the difference between the timers available in .NET.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Ok let me try System.Timers.Time – samirprogrammer Mar 15 '12 at 08:55
  • @samirprogrammer. This [question](http://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer) discuss the differences between System.Timers.Timer and System.Threading.Timer – Steve Mar 15 '12 at 14:07