Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a function every 30 seconds...

Status
Not open for further replies.

laeelin

Programmer
Jan 7, 2005
8
US
I'm writeing an application, and I want it to scan a file every 30 seconds to see if anything has changed.

I have it set up now so that I can scan the file with the click of a button, but I want a way to run this function about every 30 seconds. Every way I know to have VB wait 30 seconds will freeze the program while it's waiting.

Is there any way to have it run some code every X seconds, and still allow you to click on a "stop" button to stop the program from running?

 
Thanks, i'm not sure how I missed it when I was searching =)

 
To expand a little: set the timer interval to 1000 (i.e. 1 second), set a counter to 0, enable the timer. It will get called every 1 second. In the timer event increment the counter. When it is >= 30 disable the timer, do what you need to do, reset the counter to 0 and enable the timer.
Make sure the counter is static,public or global, so it persists between calls to the timer event.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
If its just for 30 secs, set the interval to 30000. You only need a counter if you want to exceed 65535 (just over a minute)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
is there not a way to have the function called WHEN the file is changed...

im sure i read something about this kind of thing somewhere (been out of VB a while so im not 100% sure)

If somethings hard to do, its not worth doing - Homer Simpson
 
Yes. You can use file or folder notifications (which will involve the API). I've provided several examples in this forum that a keyword search should find them

e.g.

thread222-719707 for my non-blocking version of ReadDirectoryChangesW

thread222-576673 for my (fractionally simpler) blocking version

 
If you start the scan application from VB you can determine when the scanning application closes. I have a sample available if you want to go this way
 
aha... strongm, thanks for that... i thought i was loosing my marbles... kept searching for similar on google and hitting nothing but .NET code

If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top