System.Timer Question

G

Guest

Hi.

I Have a VB.Net Windows Application that starts up using Sub Main.
It's a program that runs in the background and checks something every minute.
I use a Timer to accomplish this and it's working but to get it to work I
use a boolean variable (mbLoop) and the program runs until mbLoop is set to
False.
Do While mbLoop
'
Loop

The problem is that I see this program always using 25% of CPU. I know it's
the looping that's going on that's causing this but I don't know how to keep
the program running without it. I tried taking it out but it just executed
the code in Sub Main and when it hit End Sub the program ended.

Here's my code:
Imports System.Threading 'Timer
Module modMain
Dim WithEvents moTimer As System.Timers.Timer
Sub Main()
moTimer = New System.Timers.Timer(60000)

moTimer.AutoReset = True

moTimer.Enabled = True

'*** Will continue working as long as mbLoop is set to True
Do While mbLoop
'
Loop
End Sub

Sub OnTimedEvent(ByVal Source As Object, _
ByVal e As System.Timers.ElapsedEventArgs)
Handles
moTimer.Elapsed
'
' All my code here is executed every 60 seconds
'
End Sub

Any suggestions will be greatly appreciated!

Rita
 
G

Guest

Hi RitaG,

To free up resources while being in a loop I allways put the thread to sleep.
System.Threading.Thread.Sleep(myInteger);

Kind regards,
 
G

Guest

Thanks Rainier.

I was looking at the Sleep method without the loop but don't know which thread
to "put to sleep". I'll read up on it now.

Thanks for your response.
Rita
 
G

Guest

FYI.

After doing some research I was able to remove the loop and just added this
line instead " Thread.Sleep(System.Threading.Timeout.Infinite)".
 

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