views:

312

answers:

1

I have 2 timers in MVB 2008 Express, one to control the fading in of a window and another to call a beep function every few seconds or so. The problem is that while the window is fading in, it pauses while the timer beeps. Any ideas? Threads maybe?

+3  A: 

Without seeing your code, I have a feeling that this is indeed related to threads.

It sounds like you have a Windows Application, with two Timer controls sited on a form. You probably have code within each timer's Tick event to perform the relevant functionality, one to fade the form, the other to beep.

What is happening here is that both of these functions defined in the Tick events are running in the same thread. This is the same thread that is used for your Windows Forms UI (user interface). You can see the same problem when trying to update some portion of your form (display) while attempting to execute a long running process - Usually all form updates "stop" or "hang" until that long running process is completed.

To correct this problem, you'll need to have one (if not both) of the code functions that are called when the Timer's fire their Tick events run in a separate thread.

The easiest way to achieve this is via a BackgroundWorker component (or class). Alternatively, you can instantiate and use your own ThreadStart delegate (which is what the BackgroundWorker component does anyway!)

Something like the following:

Imports System.Threading

Public Class Form1
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim newThread As Thread
        newThread = New Thread(AddressOf DoWork)
        newThread.Start()
    End Sub

    Private Sub DoWork()
        // Long running process happens in here.
    End Sub
End Class
CraigTP
Worked great, thanks.
Shawn