Home All Groups Group Topic Archive Search About

Do nothing but wait loop..

Author
4 May 2006 4:13 PM
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.

Author
4 May 2006 7:26 PM
José_Manuel_Agüero
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.


Show quoteHide quote
"IdleBrain" <indianmostwan***@yahoo.com> escribió en el mensaje news:1146759210.614010.128740@j33g2000cwa.googlegroups.com...
| 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.
Author
5 May 2006 5:39 AM
Cor Ligthert [MVP]
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
Author
5 May 2006 11:46 AM
José_Manuel_Agüero
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.


Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> escribió en el mensaje news:e3KoCYAcGHA.3900@TK2MSFTNGP05.phx.gbl...
| 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
Author
5 May 2006 4:24 PM
IdleBrain
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
Author
5 May 2006 7:24 PM
Göran_Andersson
I'd suggest that you throw in a short Sleep in the loop also.

IdleBrain wrote:
Show quoteHide quote
> 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
>
Author
5 May 2006 11:08 PM
José_Manuel_Agüero
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.


Show quoteHide quote
"IdleBrain" <indianmostwan***@yahoo.com> escribió en el mensaje news:1146846291.920001.48850@j33g2000cwa.googlegroups.com...
| 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
Author
4 May 2006 7:45 PM
IdleBrain
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?
Author
5 May 2006 3:46 AM
Michael D. Ober
Take a look at delegates and event callbacks.  For examples, look at the
system.threading.timer class.

Mike Ober.

Show quoteHide quote
"IdleBrain" <indianmostwan***@yahoo.com> wrote in message
news:1146771914.115228.145380@j33g2000cwa.googlegroups.com...
> 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?
>
>