How to Control Timer Event?

M

Mr. B

My current app has a timer that I kick ON in my Form1_Load as follows:

' Set Up the Timer Function
Dim t As New System.Timers.Timer(12000) ' 1000 = 1 Second
t.Enabled = True ' False to Turn OFF
AddHandler t.Elapsed, AddressOf TimerFired

This in turns fires up my sub called "TimerFired" every 2 minutes...

In this sub, I check a file for a change in the date/time and if I find a
difference (ie newer file date), I then continue on with the sub and does some
updating. Otherwise I end the sub and the timer continues on with it's
periodical check.

What I want to do is to "Stop" the Timer event 'when' the sub does it's
updating... then fire it back up when the 'stuff' finishes.

I've tried a few things such as:

Timer1.Enabled = False

in my Sub... but that doesn't work...

Any ideas out there?

Regards,

Bruce
 
G

Guest

Mr B

I do some programming in java, c/c++ etc I have not done any
programming in VB yet, but I would assume the timer would execute in a
seperate thread than the main program is their a way to block/lock or suspend
the thread while processing the file?? can this be done in VB I don't know
Just a thought.

Cheers
Mitchell
 
J

Jay B. Harlow [MVP - Outlook]

Bruce,
Any reason you are doing this with a timer rather then let a
System.IO.FileSystemWatcher notify you when the file changes?

What I do is set Timer.AutoReset to False, so the event does not
automatically fire. Then at the end of the Timer.Elapsed event handler I set
a new interval & set Timer.Enabled to True. Setting the interval should not
be needed, I do it as I have varying intervals that are based on other
factors...
Dim t As New System.Timers.Timer(12000) ' 1000 = 1 Second
This in turns fires up my sub called "TimerFired" every 2 minutes...
Are you certain? It appears that your event will fire every 12 seconds.
Normally what I do is use a TimeSpan to calculate the interval.

Something like:

Dim ts As TimeSpan = TimeSpan.FromMinutes(2)
Dim t As New System.Timers.Timer(ts.TotalMilliseconds)

Or more succinctly:

Dim t As New
System.Timers.Timer(TimeSpan.FromMinutes(2).TotalMilliseconds)

Hope this helps
Jay
 
G

Guest

Define "doesn't work". That is precisely how you turn the (12-second) timer
off. Do you get an error?

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top