• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

VB.net best loop to check every 10 seconds

jameswhite1979

Senior member
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
 
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.
 
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.
 
A timer will work, but I believe your problem is that you're using the old-school Sleep command. Try Threading.Thread .Sleep() instead.
 
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....
 
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.
 
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.

 
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?
 
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.
 
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.
 
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
 
Back
Top