High processor usage

  • Thread starter Thread starter George Gita
  • Start date Start date
G

George Gita

I have the following code in a VB .NET program (version
2003):
'begin code
Sub Main()
Dim check_timer As System.Timers.Timer

While Not (check_timer Is Nothing)
'doing nothing ...
End While
End Sub
'end code

When I run the .exe file(the actual program) my processor
usage is at 100%!?
Can you tell me what's happening?
What can I do about it?
 
Hi George,

Your code is similar to this:

Dim check_timer As ArrayList

While Not (check_timer Is Nothing)
'doing nothing but going round again.
End While

In other words, the Timer aspect is irrelevant. Your loop is just going
round and round and round - without a single pause - permanently checking a
variable.

Anything that does that will use 100% CPU.

What do you want to do with this Timer?

Regards,
Fergus
 
I have the following code in a VB .NET program (version
2003):
'begin code
Sub Main()
Dim check_timer As System.Timers.Timer

While Not (check_timer Is Nothing)
'doing nothing ...
End While
End Sub
'end code

When I run the .exe file(the actual program) my processor
usage is at 100%!?
Can you tell me what's happening?
What can I do about it?

You are running a tight loop. You could try and sticking a little
Thread.Sleep in there. Something like:

While Not (check_timer Is Nothing)
Thread.Sleep(100)
End While

HTH
 
I want to use a timer without a form and that's the only way I know how to
use it.
Do you know any other ways that I could do that?
Tahnks for helping me.
 
Hi George,

You don't need to have any loops such as the one you had - that's what the
Timer's there to take care of. Here's an example.

Public Sub Foo
Dim oTimer As New System.Timers.Timer()
AddHandler oTimer.Elapsed, AddressOf TimerHasGoneOff
oTimer.Interval = 5000 'Set the Interval to 5 seconds.
oTimer.AutoReset = True 'Go off every 5 seconds
oTimer.Start 'And they're off...
End Sub

'Every time the Timer goes off...
Public Sub TimerHasGoneOff (source As Object, _
e As ElapsedEventArgs)
Console.WriteLine("Got to get busy again...")
'Do something in a timely fashion...
End Sub

Regards,
Fergus
 
Doesn't work. It just exists after the line:
oTimer.Start 'And they're off...

That's why I need the loop ...
 
Hi Gita,

Sorry, I forgot that you were doing it in Sub Main.

In that case go with Tom's idea - Thread..Sleep, but lose the Timer.

Sub Main
Do
'Do whatever you need to ...
'(includes an Exit Do somewhere)
System.Threading.Thread.Sleep (HoweverLong)
Loop
End Sub

Regards,
Fergus
 
yust a sidenote
dont forget a way to stop the loop

public blnStop as boolean = false
Sub Main
Do while not blnStop
'Do whatever you need to ...
'(includes an Exit Do somewhere)
System.Threading.Thread.Sleep (HoweverLong)
Loop
End Sub

now you can stop the loop by setting the blnStop to true

and a question dont you need to decleare threads or do doevents or something
?

eric
 
Hi George,

Although I use often that thread sleep methode, I think it is not really the
best here.
I made a sample for you with system.timers.timer
\\\
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
_
Handles MyBase.Load
Dim aTimer As New System.Timers.Timer
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Set the Interval to 5 seconds.
Me.Text = "I am to to bed, because I was up all night"
aTimer.Interval = 5000
aTimer.Enabled = True
Me.Enabled = False
End Sub
Private Sub OnTimedEvent(ByVal source As Object, ByVal e As _
System.Timers.ElapsedEventArgs)
Me.Text = "I am up again"
Dim aTimer As System.Timers.Timer = DirectCast(source, System.Timers.Timer)
aTimer.Enabled = False
Me.Enabled = True
End Sub
///

I hope this helps a little bit?

Cor
 
Howdy Eric,

There's an Exit Do in there to exit the Do. ;-))

Thread is the Class and Sleep is a shared method which makes the current
Thread sleep a while. It does the same as
System.Threading.Thread.CurrentThread.Sleep (WhatAMouthful)

DoEvents is not required (perhaps not even applicable) when there's no
message queue. You only get a message queue if you call Application.Run. And
there's no obligation to have one unless you have a Form.

What? No Form? Yep, why not? ;-) For example a Console App.

Regards,
Fergus
 
LOL, I am to bed with mine caffeine, and mine VB.NET soft toy.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Hi Gita,
That is not important, I did need that form for the example, you can change
that in form sub in Sub main, and do some other things in that timer even
procedure than me.enable.

Cor
 
Hi Cor, Gita,

The problem with the Timer idea, as Gita pointed out to me is this:

Sub Main
'Create and set the Timer.
End Sub

The program exits immediately as there is no loop to hold it in place. But
Eric's question and my answer has given me an idea.

Sub Main
'Create and set the Timer.
Application.Run()
End Sub

'This will hold the program running, even without a Form, because this
uses a message pump.

To terminate the message pump it's necessary to call Application.Exit.

So, thanks Cor and Eric for placing those pieces of the jigsaw - we've now
given Gita another choice. ;-))

Regards,
Fergus
 
THANKS everyone! I never thought that you guys would answer me so fast. It's
incredible.
 
this ng has a serious potential, and when they start to work together there
is no limit to w they can accomplish ;)
i've only recently started programming in .NET proffesionally and i'm going
to take this opportunity to thank everyone for helping me in the past (and
future) :)

so thanks all 2 :)

eric
 
Hi Cor, Gita,

The problem with the Timer idea, as Gita pointed out to me is this:

Sub Main
'Create and set the Timer.
End Sub

The program exits immediately as there is no loop to hold it in place. But
Eric's question and my answer has given me an idea.

Sub Main
'Create and set the Timer.
Application.Run()
End Sub

'This will hold the program running, even without a Form, because this
uses a message pump.

To terminate the message pump it's necessary to call Application.Exit.

So, thanks Cor and Eric for placing those pieces of the jigsaw - we've now
given Gita another choice. ;-))

Regards,
Fergus

That seems the best solution to me, Fergus. Beautiful...
 
George,
It sounds like you really want the main thread to wait for the timer thread
to finish. :-)

Rather then use a loop in the Main as every one suggested, I would recommend
you use an AutoResetEvent.

The timer routine will set the AutoResetEvent when the program is to end.
The Main routine will wait on the AutoResetEvent to be signaled.

This way the main routine will not consume ANY CPU time! (which is even less
time then using a loop & Thread.Sleep! ;-))

Something like:

Option Strict On
Option Explicit On

Module GeorgeGita

Private WithEvents timer As System.Timers.Timer
Private exitEvent As System.Threading.AutoResetEvent
Private padLock As New Object

Private count As Integer

Public Sub Main()
exitEvent = New System.Threading.AutoResetEvent(False)
timer = New System.Timers.Timer(5000)
timer.Enabled = True
exitEvent.WaitOne()
System.Threading.Thread.Sleep(1000)
End Sub

Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles timer.Elapsed
SyncLock padLock
count += 1
Debug.WriteLine(count, "timer")
If count = 5 Then
exitEvent.Set()
End If
End SyncLock
End Sub

End Module

The exitEvent.WaitOne causes the Main routine to go to sleep indefinitely
until the event is Set. The Timer_Elapsed handler waits for the 5 elapsed
event, then set the exitEvent to signaled.

The SyncLock padLock prevents the Timer Elapsed event from being entered
multiple times, in case the amount of time to handle the Elapsed event is
longer than the duration on the timer, which generally is not a good idea
anyway ;-)

The Sleep after the exitEvent.WaitOne is to prevent 'skidding' ;-). I put it
there in case there are pending Timer_Elapse events, that have not finished
processing. There are better ways to handle it, which are dependant on what
the process is really doing.

Note: Do not confuse the AutoResetEvent event with normal VB.NET events such
as Timer.Elapsed. AutoResetEvent is an object used for multi-threaded
applications so one thread can signal a second thread that something
occurred, in this case the timer is signaling main that its ok for main to
exit. Where as Timer.Elapsed is used to let one object signal a second
object that a significant event occurred (if that explanation helped at all
;-))

Hope this helps
Jay
 
Back
Top