Do nothing but wait loop..

  • Thread starter Thread starter IdleBrain
  • Start date Start date
I

IdleBrain

Hello all,
The sleep() method hangs up the application and does not respond to
events. So, I wrote a small delay loop that will allow the application
to respond to events.

'Use Delay(500) to delay the application for 500ms.

Public blnSleepTimeExpired As Boolean

Public Sub Delay(ByVal intSleepTimems As Integer)
'set interval.
Dim intCount As Integer
tmrDelay = New Timer
tmrDelay.Interval = intSleepTimems
blnSleepTimeExpired = False

tmrDelay.Start()
Do While (Not blnSleepTimeExpired)
intCount += 1
If (intCount Mod 25) = 0 Then DoEvents()
'Debug.WriteLine(intSleepTimems & " Count: " & intCount)
Loop
tmrDelay.Stop()
End Sub

Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrDelay.Tick
blnSleepTimeExpired = True
End Sub

The code works most of the time but hangs up the application once a
while.

Can anyone help me with code that would do nothing but wait for certain
time.

I appreciate any help on this issue.
Thanks.
 
The code hangs up because you are waiting intensely. The thread runs at
full speed checking all the time if something is happening. That will
impact all other processes in the computer.

Put a sleep in the loop, and it will calm down:

Do While (Not blnSleepTimeExpired)
Sleep(10)
DoEvents()
Loop
 
Hello, IdleBrain:

You should not run a tight loop with DoEvents. You should consider changing your code either
·Running your job in a separate thread. You can safely sleep it whenever you want.
·Splitting your job. See this sample:
Sub Job()
'Do something.
'Wait some time.
'Do the rest of the job.
End Sub

Converts to:

Sub Job()
'Do something.
Me.TimerJob.Interval = Sometime
Me.TimerJob.Start
End Sub

Sub TimerJob_Tick(...) handles TimerJob.Tick
Me.TimerJob.Stop
'Do the rest of the job.
End sub

Well, this last solution is rarely suitable, but you may find a variation or use BackgroundWorker or threading the way you like.

Regards.


"IdleBrain" <[email protected]> escribió en el mensaje | Hello all,
| The sleep() method hangs up the application and does not respond to
| events. So, I wrote a small delay loop that will allow the application
| to respond to events.
|
| 'Use Delay(500) to delay the application for 500ms.
|
| Public blnSleepTimeExpired As Boolean
|
| Public Sub Delay(ByVal intSleepTimems As Integer)
| 'set interval.
| Dim intCount As Integer
| tmrDelay = New Timer
| tmrDelay.Interval = intSleepTimems
| blnSleepTimeExpired = False
|
| tmrDelay.Start()
| Do While (Not blnSleepTimeExpired)
| intCount += 1
| If (intCount Mod 25) = 0 Then DoEvents()
| 'Debug.WriteLine(intSleepTimems & " Count: " & intCount)
| Loop
| tmrDelay.Stop()
| End Sub
|
| Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles tmrDelay.Tick
| blnSleepTimeExpired = True
| End Sub
|
| The code works most of the time but hangs up the application once a
| while.
|
| Can anyone help me with code that would do nothing but wait for certain
| time.
|
| I appreciate any help on this issue.
| Thanks.
 
I have implemented Tim's and Goran's ideas... They seem to be working
okay.
I cannot implement Jose's ideas because...I use the Delay() method
through out the application in the place of Sleep().

Any more thoughts to respond to the system events in a better way?
 
Take a look at delegates and event callbacks. For examples, look at the
system.threading.timer class.

Mike Ober.
 
Jose,
You should not run a tight loop with DoEvents. You should consider changing
your code either
Running your job in a separate thread. You can safely sleep it whenever you
want.
Splitting your job. See this sample:

I have read this more, can you explain to us why.

Multi.Threading takes a lot of resources and performances so there should be
a very good reason why you write this above. I have the idea that a lot of
people are just paroting, therefor why?

Cor
 
Hello Cor,

It's known that .NET 1.1 with visual styles enabled has performance problems in such situation. And DoEvents requieres you to handle things like event reentrance and forms closing in the middle of a procedure.
Multithreading takes some resources, but the performance may suffer only when you create the thread, not when you use it.
Anyway performance seems not to be a problem for IdleBrain, so he can use whatever solution he wants.

Regards.


"Cor Ligthert [MVP]" <[email protected]> escribió en el mensaje | Jose,
|
| >You should not run a tight loop with DoEvents. You should consider changing
| >your code either
| >Running your job in a separate thread. You can safely sleep it whenever you
| >want.
| >Splitting your job. See this sample:
|
| I have read this more, can you explain to us why.
|
| Multi.Threading takes a lot of resources and performances so there should be
| a very good reason why you write this above. I have the idea that a lot of
| people are just paroting, therefor why?
|
| Cor
 
Hello all,
I started using GetTickCount()..Seems to work better than the timer in
my application.
Thanks for all your comments.

Private Declare Function GetTickCount Lib "Kernel32" () As Integer

Public Sub Delay(ByVal intSleepTimems As Integer)
Dim lngTime As Long
lngTime = GetTickCount()

Do While (intSleepTimems > GetTickCount() - lngTime)
DoEvents()
Loop
End Sub
 
I'd suggest that you throw in a short Sleep in the loop also.
 
Just for completion, GetTickCount returns an UInteger. Also, your Delay sub may not work as expected if it is running when the count wraps to Integer.MinValue. This happens after ~25 days of system activity and then every ~50 days, so it's rare to affect your program, but if you use it extensively, you must know it.

Regards.


"IdleBrain" <[email protected]> escribió en el mensaje | Hello all,
| I started using GetTickCount()..Seems to work better than the timer in
| my application.
| Thanks for all your comments.
|
| Private Declare Function GetTickCount Lib "Kernel32" () As Integer
|
| Public Sub Delay(ByVal intSleepTimems As Integer)
| Dim lngTime As Long
| lngTime = GetTickCount()
|
| Do While (intSleepTimems > GetTickCount() - lngTime)
| DoEvents()
| Loop
| End Sub
 
Back
Top