Do loop memory consumption?

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

    #1

    Do loop memory consumption?

    The Timer loop below consumes all available CPU time from my development PC.
    I would like to be able to restrict its use of CPU time to minimum since
    it's not actually doing anything but counting down the time left.
    Any help is greatly appreciated.

    Bill


    Dim t As Integer = 0

    Dim Start, Finish, TotalTime As Double

    'Start = Microsoft.Visua lBasic.DateAndT ime.Timer

    Finish = Start + mSeconds ' Set end time for mseconds duration.

    Do While Microsoft.Visua lBasic.DateAndT ime.Timer < Finish

    t += 1

    lblInfo.Text = "test timer -> " & t

    lblInfo.Refresh ()

    ' Do other processing while waiting for 5mseconds to elapse.

    Loop


  • Tom Shelton

    #2
    Re: Do loop memory consumption?


    Bill Nguyen wrote:[color=blue]
    > The Timer loop below consumes all available CPU time from my development PC.
    > I would like to be able to restrict its use of CPU time to minimum since
    > it's not actually doing anything but counting down the time left.
    > Any help is greatly appreciated.
    >
    > Bill
    >
    >
    > Dim t As Integer = 0
    >
    > Dim Start, Finish, TotalTime As Double
    >
    > 'Start = Microsoft.Visua lBasic.DateAndT ime.Timer
    >
    > Finish = Start + mSeconds ' Set end time for mseconds duration.
    >
    > Do While Microsoft.Visua lBasic.DateAndT ime.Timer < Finish
    >
    > t += 1
    >
    > lblInfo.Text = "test timer -> " & t
    >
    > lblInfo.Refresh ()
    >
    > ' Do other processing while waiting for 5mseconds to elapse.
    >
    > Loop[/color]

    One way to fix this would be to insert System.Thread.S leep (100) right
    after your lblInfo.Refresh ().

    --
    Tom Shelton [MVP]

    Comment

    • Mattias Sjögren

      #3
      Re: Do loop memory consumption?

      Bill,

      Have you considered using System.Windows. Forms.Timer instead?


      Mattias

      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Bill Nguyen

        #4
        Re: Do loop memory consumption?

        Tom;

        That seemed to solve my problem. I assinged 1000 miliseconds and CPU time
        went down significantly. memory used still about the same, but that I can
        afford!
        Another question, please:
        I need to interrupt the loop when user presses the STOP button. The problem
        that btnStop(on the same form) is not accessible during the loop.
        Is there a way to check if the button was pressed during the do loop
        routine?

        Thanks again;

        Bill


        "Tom Shelton" <tom@mtogden.co m> wrote in message
        news:1146002783 .330291.234260@ j33g2000cwa.goo glegroups.com.. .[color=blue]
        >
        > Bill Nguyen wrote:[color=green]
        >> The Timer loop below consumes all available CPU time from my development
        >> PC.
        >> I would like to be able to restrict its use of CPU time to minimum since
        >> it's not actually doing anything but counting down the time left.
        >> Any help is greatly appreciated.
        >>
        >> Bill
        >>
        >>
        >> Dim t As Integer = 0
        >>
        >> Dim Start, Finish, TotalTime As Double
        >>
        >> 'Start = Microsoft.Visua lBasic.DateAndT ime.Timer
        >>
        >> Finish = Start + mSeconds ' Set end time for mseconds duration.
        >>
        >> Do While Microsoft.Visua lBasic.DateAndT ime.Timer < Finish
        >>
        >> t += 1
        >>
        >> lblInfo.Text = "test timer -> " & t
        >>
        >> lblInfo.Refresh ()
        >>
        >> ' Do other processing while waiting for 5mseconds to elapse.
        >>
        >> Loop[/color]
        >
        > One way to fix this would be to insert System.Thread.S leep (100) right
        > after your lblInfo.Refresh ().
        >
        > --
        > Tom Shelton [MVP]
        >[/color]


        Comment

        • james

          #5
          Re: Do loop memory consumption?

          Insert an "Application.Do Events: to allow for other proccesses to run. And
          then in the btnStop's click event, stop the timer.
          james

          "Bill Nguyen" <billn_nospam_p lease@jaco.com> wrote in message
          news:%23TKG6oLa GHA.3444@TK2MSF TNGP05.phx.gbl. ..[color=blue]
          > Tom;
          >
          > That seemed to solve my problem. I assinged 1000 miliseconds and CPU time
          > went down significantly. memory used still about the same, but that I can
          > afford!
          > Another question, please:
          > I need to interrupt the loop when user presses the STOP button. The
          > problem that btnStop(on the same form) is not accessible during the loop.
          > Is there a way to check if the button was pressed during the do loop
          > routine?
          >
          > Thanks again;
          >
          > Bill
          >
          >
          > "Tom Shelton" <tom@mtogden.co m> wrote in message
          > news:1146002783 .330291.234260@ j33g2000cwa.goo glegroups.com.. .[color=green]
          >>
          >> Bill Nguyen wrote:[color=darkred]
          >>> The Timer loop below consumes all available CPU time from my development
          >>> PC.
          >>> I would like to be able to restrict its use of CPU time to minimum since
          >>> it's not actually doing anything but counting down the time left.
          >>> Any help is greatly appreciated.
          >>>
          >>> Bill
          >>>
          >>>
          >>> Dim t As Integer = 0
          >>>
          >>> Dim Start, Finish, TotalTime As Double
          >>>
          >>> 'Start = Microsoft.Visua lBasic.DateAndT ime.Timer
          >>>
          >>> Finish = Start + mSeconds ' Set end time for mseconds duration.
          >>>
          >>> Do While Microsoft.Visua lBasic.DateAndT ime.Timer < Finish
          >>>
          >>> t += 1
          >>>
          >>> lblInfo.Text = "test timer -> " & t
          >>>
          >>> lblInfo.Refresh ()
          >>>
          >>> ' Do other processing while waiting for 5mseconds to elapse.
          >>>
          >>> Loop[/color]
          >>
          >> One way to fix this would be to insert System.Thread.S leep (100) right
          >> after your lblInfo.Refresh ().
          >>
          >> --
          >> Tom Shelton [MVP]
          >>[/color]
          >
          >[/color]


          Comment

          • ShaneO

            #6
            Re: Do loop memory consumption?

            Bill Nguyen wrote:[color=blue]
            > Tom;
            >
            > That seemed to solve my problem. I assinged 1000 miliseconds and CPU time
            > went down significantly. memory used still about the same, but that I can
            > afford!
            > Another question, please:
            > I need to interrupt the loop when user presses the STOP button. The problem
            > that btnStop(on the same form) is not accessible during the loop.
            > Is there a way to check if the button was pressed during the do loop
            > routine?
            >[/color]

            Hello Bill,

            I'd consider changing you code as follows (Watch for line-wrapping) -

            Dim blExitLoop as Boolean = False 'Place this in General Declarations
            Section

            Dim t As Integer = 0
            Dim Start, Finish, TotalTime As Double
            'Start = Microsoft.Visua lBasic.DateAndT ime.Timer
            Finish = Start + mSeconds ' Set end time for mseconds duration.
            Do While Microsoft.Visua lBasic.DateAndT ime.Timer < Finish And blExitLoop
            = False
            t += 1
            lblInfo.Text = "test timer -> " & t
            'lblInfo.Refres h() 'You can REMOVE this now!
            System.Threadin g.Thread.Sleep( 100)
            System.Windows. Forms.Applicati on.DoEvents()

            ' Do other processing while waiting for 5mseconds to elapse.
            Loop

            (Note the change to your "Do While... Loop above!)

            I'd then put the following in your "Stop" Button Click Event -

            blExitLoop = True


            I hope this helps.

            ShaneO

            There are 10 kinds of people - Those who understand Binary and those who
            don't.

            Comment

            • Bill nguyen

              #7
              Re: Do loop memory consumption?

              This will definitely help!
              My thanks to all of you in this thread.

              Bill


              "ShaneO" <shanearn@optus net.com.au> wrote in message
              news:444ecf49$0 $3341$afc38c87@ news.optusnet.c om.au...[color=blue]
              > Bill Nguyen wrote:[color=green]
              >> Tom;
              >>
              >> That seemed to solve my problem. I assinged 1000 miliseconds and CPU time
              >> went down significantly. memory used still about the same, but that I can
              >> afford!
              >> Another question, please:
              >> I need to interrupt the loop when user presses the STOP button. The
              >> problem that btnStop(on the same form) is not accessible during the
              >> loop.
              >> Is there a way to check if the button was pressed during the do loop
              >> routine?
              >>[/color]
              >
              > Hello Bill,
              >
              > I'd consider changing you code as follows (Watch for line-wrapping) -
              >
              > Dim blExitLoop as Boolean = False 'Place this in General Declarations
              > Section
              >
              > Dim t As Integer = 0
              > Dim Start, Finish, TotalTime As Double
              > 'Start = Microsoft.Visua lBasic.DateAndT ime.Timer
              > Finish = Start + mSeconds ' Set end time for mseconds duration.
              > Do While Microsoft.Visua lBasic.DateAndT ime.Timer < Finish And blExitLoop =
              > False
              > t += 1
              > lblInfo.Text = "test timer -> " & t
              > 'lblInfo.Refres h() 'You can REMOVE this now!
              > System.Threadin g.Thread.Sleep( 100)
              > System.Windows. Forms.Applicati on.DoEvents()
              >
              > ' Do other processing while waiting for 5mseconds to elapse.
              > Loop
              >
              > (Note the change to your "Do While... Loop above!)
              >
              > I'd then put the following in your "Stop" Button Click Event -
              >
              > blExitLoop = True
              >
              >
              > I hope this helps.
              >
              > ShaneO
              >
              > There are 10 kinds of people - Those who understand Binary and those who
              > don't.[/color]


              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Do loop memory consumption?

                [color=blue]
                > For stuff like this... you should be using a Windows Timer to process
                > repeatitve GUI events.[/color]

                Serious why, I have the idea that once somebody wrote this on a blog and no
                everybody is parroting it. In my opinion is for UI (forms) application the
                forms timer excellent.

                But I can be wrong.

                Cor

                Cor


                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: Do loop memory consumption?

                  [color=blue]
                  > For stuff like this... you should be using a Windows Timer to process
                  > repeatitve GUI events.[/color]

                  Why? see for the rest my reply to Mattias.

                  Cor


                  Comment

                  • Göran Andersson

                    #10
                    Re: Do loop memory consumption?

                    > The Timer loop below consumes all available CPU time from my development PC.[color=blue]
                    > I would like to be able to restrict its use of CPU time to minimum since
                    > it's not actually doing anything but counting down the time left.[/color]

                    Use Thread.Sleep to make a delay without using CPU time.

                    Comment

                    • Mattias Sjögren

                      #11
                      Re: Do loop memory consumption?

                      [color=blue]
                      > I need to interrupt the loop when user presses the STOP button. The problem
                      >that btnStop(on the same form) is not accessible during the loop.[/color]

                      That's exactly why you shouldn't sleep on a UI thread. And DoEvents
                      has its own problems. Again, try it with a timer.


                      Mattias

                      --
                      Mattias Sjögren [C# MVP] mattias @ mvps.org
                      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                      Please reply only to the newsgroup.

                      Comment

                      • Mattias Sjögren

                        #12
                        Re: Do loop memory consumption?

                        [color=blue]
                        >In my opinion is for UI (forms) application the
                        >forms timer excellent.[/color]

                        I was under the impression that Bill's code is in a winforms
                        application. So I'm not sure if you're disagreeing with us or what
                        your point is.


                        Mattias

                        --
                        Mattias Sjögren [C# MVP] mattias @ mvps.org
                        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                        Please reply only to the newsgroup.

                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: Do loop memory consumption?

                          >[color=blue][color=green]
                          >>In my opinion is for UI (forms) application the
                          >>forms timer excellent.[/color]
                          >
                          > I was under the impression that Bill's code is in a winforms
                          > application. So I'm not sure if you're disagreeing with us or what
                          > your point is.
                          >[/color]
                          Forget it, I just misreaded your messages, we agree completely.

                          Sorry

                          :-)

                          Cor


                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: Do loop memory consumption?

                            Spam Catcher,

                            The same as to Mattias,

                            I misreaded your messages I thougt you were pointing to the
                            system.systems. timer as I had seen done more, while the forms timer is in my
                            idea sufficient in this case.

                            So I thought maybe they have a reason for that and than I want to know why.

                            Sorry,

                            Cor.

                            "Spam Catcher" <spamhoneypot@r ogers.com> schreef in bericht
                            news:Xns97B1843 E472C7usenethon eypotrogers@127 .0.0.1...[color=blue]
                            > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in
                            > news:uHinjNPaGH A.4916@TK2MSFTN GP04.phx.gbl:
                            >[color=green]
                            >> Serious why, I have the idea that once somebody wrote this on a blog
                            >> and no everybody is parroting it. In my opinion is for UI (forms)
                            >> application the forms timer excellent.[/color]
                            >
                            > There are 3 timers in .NET - each one for a specific use.
                            >
                            > So Timers could be used in forms, in server code, or as a general timer.
                            > Depends on which class and which scenario.
                            >
                            > Better than having a constant loop.
                            >
                            > Of course it depends on the situation - some things are better suited for
                            > a
                            > thread looping and others for a timer.[/color]


                            Comment

                            • Bill nguyen

                              #15
                              Re: Do loop memory consumption?

                              I'm willing to learn.
                              Can you please give me a sample code?
                              Thanks

                              Bill

                              "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                              news:%23360uiLa GHA.4248@TK2MSF TNGP05.phx.gbl. ..[color=blue]
                              > Bill,
                              >
                              > Have you considered using System.Windows. Forms.Timer instead?
                              >
                              >
                              > Mattias
                              >
                              > --
                              > Mattias Sjögren [C# MVP] mattias @ mvps.org
                              > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                              > Please reply only to the newsgroup.[/color]


                              Comment

                              Working...