Do nothing but wait loop..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IdleBrain

    #1

    Do nothing but wait loop..

    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 blnSleepTimeExp ired As Boolean

    Public Sub Delay(ByVal intSleepTimems As Integer)
    'set interval.
    Dim intCount As Integer
    tmrDelay = New Timer
    tmrDelay.Interv al = intSleepTimems
    blnSleepTimeExp ired = False

    tmrDelay.Start( )
    Do While (Not blnSleepTimeExp ired)
    intCount += 1
    If (intCount Mod 25) = 0 Then DoEvents()
    'Debug.WriteLin e(intSleepTimem s & " Count: " & intCount)
    Loop
    tmrDelay.Stop()
    End Sub

    Private Sub tmrDelay_Tick(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles tmrDelay.Tick
    blnSleepTimeExp ired = 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.

  • Tim

    #2
    Re: Do nothing but wait loop..

    a bit of vb6 code, but it may work

    ti=timer
    do
    doevents
    loop until timer>ti+1

    Comment

    • Göran Andersson

      #3
      Re: Do nothing but wait loop..

      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 blnSleepTimeExp ired)
      Sleep(10)
      DoEvents()
      Loop


      IdleBrain wrote:[color=blue]
      > 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 blnSleepTimeExp ired As Boolean
      >
      > Public Sub Delay(ByVal intSleepTimems As Integer)
      > 'set interval.
      > Dim intCount As Integer
      > tmrDelay = New Timer
      > tmrDelay.Interv al = intSleepTimems
      > blnSleepTimeExp ired = False
      >
      > tmrDelay.Start( )
      > Do While (Not blnSleepTimeExp ired)
      > intCount += 1
      > If (intCount Mod 25) = 0 Then DoEvents()
      > 'Debug.WriteLin e(intSleepTimem s & " Count: " & intCount)
      > Loop
      > tmrDelay.Stop()
      > End Sub
      >
      > Private Sub tmrDelay_Tick(B yVal sender As System.Object, ByVal e As
      > System.EventArg s) Handles tmrDelay.Tick
      > blnSleepTimeExp ired = 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.
      >[/color]

      Comment

      • José Manuel Agüero

        #4
        Re: Do nothing but wait 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.Int erval = Sometime
        Me.TimerJob.Sta rt
        End Sub

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

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

        Regards.


        "IdleBrain" <indianmostwant ed@yahoo.com> escribió en el mensaje news:1146759210 .614010.128740@ j33g2000cwa.goo glegroups.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 blnSleepTimeExp ired As Boolean
        |
        | Public Sub Delay(ByVal intSleepTimems As Integer)
        | 'set interval.
        | Dim intCount As Integer
        | tmrDelay = New Timer
        | tmrDelay.Interv al = intSleepTimems
        | blnSleepTimeExp ired = False
        |
        | tmrDelay.Start( )
        | Do While (Not blnSleepTimeExp ired)
        | intCount += 1
        | If (intCount Mod 25) = 0 Then DoEvents()
        | 'Debug.WriteLin e(intSleepTimem s & " Count: " & intCount)
        | Loop
        | tmrDelay.Stop()
        | End Sub
        |
        | Private Sub tmrDelay_Tick(B yVal sender As System.Object, ByVal e As
        | System.EventArg s) Handles tmrDelay.Tick
        | blnSleepTimeExp ired = 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.

        Comment

        • IdleBrain

          #5
          Re: Do nothing but wait loop..

          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?

          Comment

          • Michael D. Ober

            #6
            Re: Do nothing but wait loop..

            Take a look at delegates and event callbacks. For examples, look at the
            system.threadin g.timer class.

            Mike Ober.

            "IdleBrain" <indianmostwant ed@yahoo.com> wrote in message
            news:1146771914 .115228.145380@ j33g2000cwa.goo glegroups.com.. .[color=blue]
            > 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?
            >
            >[/color]



            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Do nothing but wait loop..

              Jose,
              [color=blue]
              >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:[/color]

              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


              Comment

              • José Manuel Agüero

                #8
                Re: Do nothing but wait loop..

                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]" <notmyfirstname @planet.nl> escribió en el mensaje news:e3KoCYAcGH A.3900@TK2MSFTN GP05.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

                Comment

                • IdleBrain

                  #9
                  Re: Do nothing but wait loop..

                  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

                  Comment

                  • Göran Andersson

                    #10
                    Re: Do nothing but wait loop..

                    I'd suggest that you throw in a short Sleep in the loop also.

                    IdleBrain wrote:[color=blue]
                    > 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
                    >[/color]

                    Comment

                    • José Manuel Agüero

                      #11
                      Re: Do nothing but wait loop..

                      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.MinValu e. 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" <indianmostwant ed@yahoo.com> escribió en el mensaje news:1146846291 .920001.48850@j 33g2000cwa.goog legroups.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

                      Comment

                      Working...