Familiar Slight of Hand with the Timer Control

Visual Basic 6 programmers learned how to get a lot of mileage out of the Timer control. VB .NET has a Timer too, and to help provide you with a familiar footing we will start with using the Timer in VB .NET.

Using the Timer control is not multithreaded programming. The Timer control relies on the event model. The Timer control hooks into the operating system's timer ”the same clock that drives your CPU ”and keeps track of ticks. The ticks are used to measure the passage of time, and the Timer.Interval property is compared to how much time has passed. When the interval has elapsed, the Timer control raises the Tick event. If you write an event handler for the Tick event, you can perform background tasks at regular intervals.

TIP

Double-click on any control to generate the default event handler.

To use the Timer control, select the Timer from the toolbox in a Windows Forms application, and place the Timer control into the component tray in the designer. (All controls that are not visible at runtime are placed in the component tray.) Double-click on the Timer to generate an event handler for the Tick event.

Keep in mind that the Timer.Tick event occurs on the same thread as the rest of the code in a Windows Forms application. That means that other code is waiting while the Tick event handler code is running. For this reason, if you put too much code in the Timer.Tick event handler, your application's performance will deteriorate. That's why the Timer is really good only for lightweight background tasks. A simple example is to use a Timer to display the date and time in a status bar. (Refer to the TimerDemo.sln file available with this book.)

Create a time-keeping status bar by adding a StatusBar control to a Windows Form and a Timer control to the component tray for that form. Double-click on the Timer control to generate the Tick event handler and add code to set the date and time. Listing 6.1 shows the Tick event handler from the TimerDemo.sln main form.

Listing 6.1 Appropriately Demonstrative Code for a Timer.Tick Event
 Private Sub Timer1_Tick(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles Timer1.Tick   StatusBar1.Text = DateTime.Now End Sub 

You can satisfy yourself that the Timer is not using a separate thread ”that is, it is not multithreaded ”by adding a breakpoint on the StatusBar1. Text = DateTime.Now statement. When the application breaks, select DebugWindowsThreads, and you will see that only one thread is running (Figure 6.1). You can also call the Control.InvokeRequired method. Controls inherit InvokeRequired ; this method returns True if the thread it is called from is different from the thread the control is on. You will need to know about InvokeRequired for asynchronous and multithreaded processing.

Figure 6.1. The Threads window shows the running threads.

graphics/06fig01.gif



Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net