All,
Following is a simplified snippet from a program I am currently developing:
Imports System.Threading
While this approach is not ideal (none of my attempts with various locking mechanisms worked well), this does get the job done. But. For some reason that i do not understand, the Thread.Sleep() call in Btn_Click causes myTimer_Tick to sleep as well. I have verified this by inserting Console.Write statements into myTimer_Tick.Code:Public Class myClass Dim bIsRunning As Boolean = False Dim myTimer As Threading.Timer Private Sub btn_Click(sender As System.Object, e As System.EventArgs) Handles btn.Click If Not bIsRunning Then bIsRunning = True myTimer = New Threading.Timer(AddressOf myTimer_Tick, Nothing, 0, Timeout.Infinite) Else bIsRunning = False Thread.Sleep(3000) myTimer.Dispose() myTimer = Nothing End If End Sub Private Sub myTimer_Tick(ByVal state As Object) <do stuff, including updating the form on which btn resides> If bIsRunning Then waitTime = <calculate value> myTimer.Change(waitTime, Timeout.Infinite) End If End Sub End Class
I was under the impression that the Threading.Timer ran in its own thread, thus a Thread.Sleep in the parent thread should not affect it. Obviously, my impression is wrong. Can you please a) point me to something that explains this behavior and b) suggest an approach that obviates this problem.
Thank you.




Reply With Quote
