Example Source Code Tutorial #5

VB6, VB.NET & C# Timers

 

Why use Timers?

In automation you have all kinds of things that have to be done in parallel (asynchronously).  You could write several separate VB or C# programs to handle each event but then trying to get all these separate programs to share data is a mess.  Threading in VB6 is a mess.  Using timers is an easy way to simulate a multi-tasking operating system. 

So, you'll try using a bunch of timer controls with each timer reminding you to handle an external event.  For example one timer to read your I/O, a second timer to write to your I/O, a third to read and write serial devices, a fourth to check for alarms, a fifth to log data every five seconds and a bunch of other timers to handle all of the other asynchronous events.  What could be easier?  

 

VB6

VB6 has timer controls that allow you to configure each timer with an interval and at the end of that interval the timer's event (subroutine) is called.  In the help file for the Timer control Microsoft even gives you the advice "There is no practical limit on the number of active timer controls you can have in Visual Basic running under Windows".  What could be better?  

Download this example code and see what problems you will (sooner or later) experience with only two timers.  Things get much uglier with more than two timers plus other events such as serial communications. 

The VB6 example program shows you that:

1. VB6 timer controls will not execute if the MsgBox is displayed.  Therefore, never use the MsgBox in VB6 -- create your own message box (this will be a later example).  If you use message box then all your other code stops executing until the MsgBox is cleared.  

2. Timer controls will not execute if you are running a lot of code unless you insert the DoEvents command.  DoEvents basically pauses the subroutine you are running and lets other subroutines use the processor (CPU).  

In our experience, with VB6,  we have found that using only one timer is usually the best approach.  We keep a counter in the Timer event of how many times the event has been called and we call a separate routine each time.  For example, the first time the Timer event is called we might send a message to the PLC requesting to read data.  The second time the timer event is called we go read the buffer to retrieve the data read from the PLC.  The third time the Timer event is called we go execute control logic. The fourth time we check alarms. The fifth time we write data to the PLC.  The sixth time we check the response back from the PLC.  

The DoEvents command is very important in automation projects and should be used in any routine that takes a lot of processor time.  

 

.NET

There are three types of timers in .NET: Windows Forms, Server, and Thread Timers.  There are advantages and disadvantages to each. 

For an overview of .NET Windows Forms timer click here

For an overview of .NET server timers click here

For an overview of .NET threading timers click here

The biggest drawback to the .NET Windows Forms Timer control is that if you stop the timer (using the Enabled Property or Stop method) then the timer can be garbage collected and removed from your program.  Then if you want to use it again -- it no longer exists and your program crashes (or does funny things) if you try to access it. 

The problem is that timers can overrun each other (we forgot the technical term) in handlers that take a long time to execute.  In other words, if the timer goes off, calls the event handler, and before the event handler can finish, the timer goes off again, and queues another call of the event handler. 

The obvious solution is to stop the timer at the beginning of the event handler and then start it at the end.  But the Windows Forms timer is then subject to garbage collection if you do that! 

Server timers are cool because you can set the AutoRestart property to False.  When it goes off, it calls your event handler, and at the end of the event handler, you simply call the Start method to restart the timer. 

Threads in .NET make programming easier / more difficult depending on whether you know how to use them or not.  We will not discuss threads in this example, but will discuss them later.  For now we refer you to this book and this book

 

Download Options

  1. Click here to download the Visual Basic 6 source code for this example.
  2. Click here to download the VB.NET source code using Windows Forms Timer. 
  3. Click here to download the VB.NET source code using Server Timer. 
  4. Click here to download the C# source code using Windows Forms Timer. 
  5. Click here to download the C# source code using Server Timer. 
  6. Click here to download the ASP.NET source code for this example. 

 

We try to offer a fair and balanced opinion on every page of our website.  We would appreciate more information from other users to express their opinions which we will then incorporate.  If you have questions or comments please post them on our message board (see button in left hand column) so that others can read and benefit. 

visual basic vb6 C# chsarp VB.net example source code tutorial timers