Question Timer Help

Ian W

Active member
Joined
Feb 11, 2010
Messages
31
Programming Experience
1-3
Hi,

I've almost completed my first VB.Net app which is a rewrite of an old vba app I wrote :)

The last part that I need to write is the timed events part.

What I have in VBA is quite straightforward..

Every 500ms the Timer event runs through, it checks if certain variables have been flagged and if so runs the relevent code? Code is nothing major, just updates a networked device.

I've looked into timers in .net but it seems like there are various routes that I can go down. I'm assuming Forms.Timer is the most appropiate here.

If anyone can point me in the direction of a good beginners guide to timers that would be great. Thanks
 
You can use either Forms.Timer or the System.Timers.Timer
You will set the Interval in milliseconds(1000 = 1 sec)

With Windows.Forms.Timer you can drag it from the toolbox, and you will use the Tick event that fires for the interval set.

With the System.Timers.Timer you can add it to the toolbox(not there by default), and you will use the Elapsed event and set the AutoReset property to True so it keeps firing for each Interval set.
 
Running anything in a timer equals something always running in the background slowing down your PC. I dont understand why you just cant execute your code whenever your value changes. Somewhere in your coding you are setting these values/flags, simply put an if statement afterwards to run your check and then execute your update if needed without the need of a timer constantly running.
 
I'm certainly open to idea's ??

Timer currently does the following.

I have a Global named cInputTimer.

cInputTimer is increased by 500 every 500ms.

I currently check every 10 minutes (using If cInputTimer = 60000) if the current recorded input is equal to the last input recorded 10 minutes earlier, if it is then it puts the network device into 'sleep'.

The rest of the timer is set to fire if a condition is met, if I could figure out how to do a Do...Loop but hold for 5 seconds before each loop then I could probably get rid of the timer - is this possible?
 
Holding for 5 seconds would freeze the UI. Just for fun I ran a side-by-side comparison of the system timer and the forms timer. Both set at 1000ms.

1st test; nothing in the tick/elapsed events except updating a variable counter,
results: at 160 seconds you could visibly see the form timer lagging ~ .25 seconds.

2nd test; a do/loop to move the form's width back and forth,
results: freezes the UI b/t moving width back and forth and the forms timer was now 1/2 the value of the system timer.

3rd test; add Application.DoEvents in both loops,
result: currently 1000+ seconds into test and there is no difference b/t the output.

4th test; put the mean loops into the system timers elapsed event,
result: it did not slow it down at all even with the application.DoEvent commented out.
 
Last edited:
What my example shows is the more stuff you have in the tick event the more you can block the UI. Is there anything that changes the cInputTimer variable other than the timer? It sounds like you have made a program idle time. If not you could get the timer to just fire at 10 minutes - much less UI disturbing.
 
Okay so I have done away with the cInput timer counter and I just use the DateDiff function to check for elasped minuites between input, this works great !

Newguy - Sorry if what you have put is answering my next question but i'm a complete noob when it comes .net :eek:

Is there anyway I can flag an event as happening (in my case an input error) and then 10 seconds later run some a procedure?

The reasoning behind this is that when a user trys to make an invalid input the network device displays 'Error Unknown Input' but after say 10 - 20 seconds it wish to send another networkstream command to revert it to the previous screen.
 
I suppose you are handling the error in a Try/Catch, in the Catch block Start the timer (10-20 sec). Then in the tick event run your new command and disable the timer.
 
I suppose you are handling the error in a Try/Catch, in the Catch block Start the timer (10-20 sec). Then in the tick event run your new command and disable the timer.

Yes, thats what I have been trying to do but its not working ?

I have the following set.

Timer called TicToc

Timer Properties in design view set as Enabled = False

On Error Catch I have TicToc.Start()

Nothing happens though? The timer is not started?
 
Set the timer's enabled property to True in the Catch block and it will start automatically.
 
Last edited:
Back
Top