VB.net best loop to check every 10 seconds

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
I have a snippet of code that needs to check every 10 seconds to see if a program is running. This is the code I currently have:

Dim loopcounter As String

loopcounter = "0"
Do Until loopcounter = "1"
Sleep(10000)
If Diagnostics.Process.GetProcessesByName("notepad").Length >= 1 Then
MessageBox.Show("notepad IS running")
Else
loopcounter = "1"
MessageBox.Show("notepad is Not running")
End If
Loop

Is this a good way I ask because it 'hangs' my app when checking as part of the loop or is it just the process or checking for the program?

TIA , J
 

Noobsa44

Member
Jun 7, 2005
65
0
0
Unless you are doing something really complicated, I would say you should use a timer set for every 10 seconds and just run the check every 10 seconds. That way you don't need the loop (you simply stop the timer when the program is found) and you don't see the UI "hanging" while it sits in the loop/sleeps. You could also use threading, but I wouldn't mess with threading unless you already feel comfortable with threads or are willing to invest the time to learn up on them.
 

ItsPat

Senior member
Jun 22, 2003
288
0
71
If you are using the IDE just go to Toolbox then Components and there is a little stopwatch icon and its called Timer. If you go to properties you can set it to the length of time you need.
 

SearchMaster

Diamond Member
Jun 6, 2002
7,791
114
106
A timer will work, but I believe your problem is that you're using the old-school Sleep command. Try Threading.Thread .Sleep() instead.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: SearchMaster
A timer will work, but I believe your problem is that you're using the old-school Sleep command. Try Threading.Thread .Sleep() instead.

That's still going to block his program....
 

SearchMaster

Diamond Member
Jun 6, 2002
7,791
114
106
Originally posted by: Crusty
Originally posted by: SearchMaster
A timer will work, but I believe your problem is that you're using the old-school Sleep command. Try Threading.Thread .Sleep() instead.

That's still going to block his program....

Ah, good point, I misread his problem. I thought he was complaining about his program using CPU cycles during the sleep.
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: ItsPat
If you are using the IDE just go to Toolbox then Components and there is a little stopwatch icon and its called Timer. If you go to properties you can set it to the length of time you need.

This is the answer.

 

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Ok I have started to play with the timer. I added the timer from toolbox and code:

Dim sec As Integer

Timer1.Enabled = True
Timer1.Interval = 60000

Do Until sec = 10

MessageBox.Show("Timer test")
sec = sec + 1

Loop

MessageBox.Show("Timer test completed after 10 checks")

This is not using the timer its just running through the loop and adding 1 to the value of sec till it reaches 10 and exits the loop.

So how do I make the loop wait a minute before it runs each time?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
A timer generates an interrupt to execute a chunk of code that was identified when the timer was activated. Take the code thatis inside the loop and create a seperate method. When starting the timer,reference the new method.


then get rid of the loop. Where you were looping - return back to the overallprogram handler. When themethod that is triggered by the timer processes, it needs to then follow up any code within and after the spot where the loop was.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
The idea behind a timer is that some other thread that is executing is keeping track of your timer/interval and at the appropriate time it will call a function you specified by assigned an event handler to the event of the Timer object.
 

JACKDRUID

Senior member
Nov 28, 2007
729
0
0
Originally posted by: jameswhite1979
Ok I have started to play with the timer. I added the timer from toolbox and code:

Dim sec As Integer

Timer1.Enabled = True
Timer1.Interval = 60000

Do Until sec = 10

MessageBox.Show("Timer test")
sec = sec + 1

Loop

MessageBox.Show("Timer test completed after 10 checks")

This is not using the timer its just running through the loop and adding 1 to the value of sec till it reaches 10 and exits the loop.

So how do I make the loop wait a minute before it runs each time?

you do not use loop with timer control...here is what you do

1. drag timer onto your form
2. set timer tick property to enabled, 1000 ms(10s)

then on each timer tick(10s set by step 2)...

Private Sub NameOfEventTick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YourTimer.Tick
If Diagnostics.Process.GetProcessesByName("notepad").Length >= 1 Then
YourTimer.Enabled = False
End If
End Sub