Ok so first off I am using visual basic 2010 and I have a thread that continuously loops to check for network requests from the remote clients. What I would like to do is when a client submits a certain request a timer will be triggered to start a countdown on the form, but for some reason I can't get the timer to start at all while the thread is running. I have also tried to abort the thread before starting the timer and that doesn't work either. Any help would be appreciated because I have been banging my head against the wall about this since last week.
Do While p1buzzedstatus = False Or p2buzzedstatus = False Or p3buzzedstatus = False
'Download Communication File'
Try
Threading.Thread.Sleep(1)
Dim Aclient As New Net.WebClient
Aclient.Credentials = New Net.NetworkCredential(remote_buzzer_registration.FTPUserName, remote_buzzer_registration.FTPPassword)
If Aclient.DownloadString(BuzzerCommandURL) = "" Then
'Don't Do Anything'
Else
'Check For Buzz'
keypressed = Aclient.DownloadString(BuzzerCommandURL)
Aclient.UploadString(BuzzerCommandURL, "")
buzzercode()
answer_timer.Start() ' <--- HERE
End If
Catch ex As Exception
End Try
Loop
If your answer_timer is a WinForm timer control (System.Windows.Forms.Timer), then you cannot start it from any thread other than the UI thread. Starting a WinForm timer control from another thread will not cause an exception, but it will not work at all either.
To start the timer properly, you can invoke the method on the UI thread like this:
Me.Invoke(New MethodInvoker(AddressOf answer_timer.Start))
Alternatively, you could use either the System.Timers.Timer or the System.Threading.Timer. Either of those work fine from any thread. The one in System.Timers is the closest equivalent to the control. However, since these other two timers tick via a new thread, the code that executes upon the timer tick will still need to invoke back to the UI thread before doing anything to the UI. So, if your timer tick code is doing anything to the form or controls, it's probably easier to just stick with the WinForm timer control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With