How can i avoid 100% cpu usage?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vidyareddy
    New Member
    • Sep 2007
    • 8

    How can i avoid 100% cpu usage?

    i have developed a speech processing exe (31aug.xe) which listen to what u speak and write it in a file called OUTPUT.Accordin g to the contents in file called CHECK,picture shud change.Display should change only when there is "b' in DISPLAYfile.oth erwise,old display must remain.Now i want the GUI in VB.net 2 to display the result as soon as the file contents change.Please see the code below

    [CODE=vbnet]Shell("31aug.ex e", vbNormalFocus)
    'System.Threadi ng.Thread.Sleep (5000)
    'this waits till user speaks something.

    Dim iofile As New StreamReader("D ISPLAY")
    Dim ioline As String
    ioline = iofile.ReadLine
    iofile.Close()
    Dim i As Integer
    i = 0

    While i >= 0
    If i > 0 Then
    Me.Update()
    End If

    If ioline = "b" Then

    Dim iofile1 As New StreamReader("O UTPUT")
    Dim ioline1 As String
    ioline1 = iofile1.ReadLin e
    iofile1.Close()
    mainTB.Text = ioline1

    Dim iofile2 As New StreamReader("C HECK")
    Dim ioline2 As String
    ioline2 = iofile2.ReadLin e
    iofile2.Close()
    If ioline2 = "Dusheri" Then
    PictureBox1.Ima ge = My.Resources.ma ngo1
    Else
    If ioline2 = "Orange" Then
    PictureBox1.Ima ge = My.Resources.or ange
    Else
    If ioline2 = "Maize" Then
    PictureBox1.Ima ge = My.Resources.ma ize
    Else
    If ioline2 = "SonaMahsur i" Then
    PictureBox1.Ima ge = My.Resources.Ri ce
    Else
    If ioline2 = "Fine" Then
    PictureBox1.Ima ge = My.Resources.su gar
    Else
    If ioline2 = "Bajra" Then
    PictureBox1.Ima ge = My.Resources.ba jra

    End If
    End If
    End If
    End If

    End If
    End If

    i = i + 1
    'MsgBox("shall we go again?")
    End If
    End While[/CODE]

    I wrote this code in form load event.Now,this is working fine as long as i include this last message box.But if i remove,its showing 100% cpu usage .Please help me how to solve this.
    Last edited by Killer42; Sep 4 '07, 02:48 AM. Reason: Added [COPE=vbnet] tag.
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by vidyareddy
    [CODE=vb]
    While i >= 0
    If i > 0 Then
    Me.Update()
    End If[/CODE]
    I wrote this code in form load event.Now,this is working fine as long as i include this last message box.But if i remove,its showing 100% cpu usage .Please help me how to solve this.
    I think that is because of This part of code (If i = 1 then it will not exit the while loop).

    Can you explain why you are using this

    Comment

    • Robbie
      New Member
      • Mar 2007
      • 180

      #3
      If you're using VB6, all you need to do is add a line of code: [code=vb]DoEvents[/code]
      Just before your While loop ends or just after it starts. This lets the program 'keep responding' (to Windows thread messages), and also lets it keep responding to keyboard and mouse clicks, as if you weren't even in a loop. I think you'll find that useful. ;)

      Also, I seriously recommend you use the Select Case method instead of if else if else if else etc. It makes it so much easier, for example you don't need to pile a load of end ifs on the end.

      If for example you use this:
      [code=vb]
      if colour="Yellow" then
      MsgBox "It's yellow"
      else
      if colour="Red" then
      MsgBox "It's red"
      else
      if colour="Blue" then
      MsgBox "It's blue"
      else
      if colour="Green" then
      MsgBox "It's green"
      end if
      end if
      end if
      end if
      [/code]
      You could do this, which is much neater and much more easy-to-follow:
      [code=vb]
      Select Case colour
      case "Yellow"
      MsgBox "It's yellow"
      case "Red"
      MsgBox "It's red"
      Case "Blue"
      MsgBox "It's blue"
      Case "Green"
      MsgBox "It's green"
      End Select
      [/code]

      Hope it helps! =D
      Let me know how you get on!

      Comment

      • vidyareddy
        New Member
        • Sep 2007
        • 8

        #4
        @hariharanmca

        sir,i have to check continuously for changes in OUTPUT.so,i have this code in a while loop.I want to display new output as soon as the exe writes into OUTPUT file.

        Comment

        • vidyareddy
          New Member
          • Sep 2007
          • 8

          #5
          @ robbie
          sir,
          thanks for select code.It makes code simpler.I am using VB.NET 2005 express edition.Please suggest me something.

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            Originally posted by vidyareddy
            @hariharanmca

            sir,i have to check continuously for changes in OUTPUT.so,i have this code in a while loop.I want to display new output as soon as the exe writes into OUTPUT file.
            So, you have to use timer control(like multi threading) it will not affect your CPU.

            (Just avoid multiple elseIF as Robbie pointed).

            Comment

            • Robbie
              New Member
              • Mar 2007
              • 180

              #7
              Originally posted by hariharanmca
              So, you have to use timer control(like multi threading)
              Yes, vidyareddy, that is something you could try instead.
              If you add a timer to your form and double-click on it, I think on .NET that will take you to editing code for the timer's Tick event.
              If you put all the code inside the while loop into that Tick event instead, that is an alternative.

              If you'd still like to do it in the While loop, you'll probably be glad to know that I just found the equivalent of DoEvents in VB6!
              In .NET, it's just Application.DoE vents()
              Hope it helps! =D

              Comment

              • vidyareddy
                New Member
                • Sep 2007
                • 8

                #8
                sir,
                i have included the code in timer_tick event.
                It is working fine only when i include a msgbox.otherwis e,its taking 100%cpu usage again.
                I am able to see the results after every 5sec using timer but only problem being msgbox appearing over and over.
                Is there anyway that this messagebox disappear automatically?


                Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
                Shell("31aug.ex e", vbNormalFocus)
                System.Threadin g.Thread.Sleep( 4000)
                'this allows time for level measurement
                Timer1.Interval = 6000
                'timer starts
                Timer1.Enabled = True

                End Sub

                Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Timer1.Tick
                Dim i As Integer = 0
                Dim iofile As New StreamReader("D ISPLAY")
                Dim ioline As String
                ioline = iofile.ReadLine
                iofile.Close()
                While i >= 0
                If i > 0 Then
                Me.Update()
                End If

                If ioline = "b" Then

                Dim iofile1 As New StreamReader("O UTPUT")
                Dim ioline1 As String
                ioline1 = iofile1.ReadLin e
                iofile1.Close()
                mainTB.Text = ioline1
                Dim iofile2 As New StreamReader("C HECK")
                Dim ioline2 As String
                ioline2 = iofile2.ReadLin e
                iofile2.Close()
                Select Case ioline2
                Case "Dusheri"
                PictureBox1.Ima ge = My.Resources.ma ngo1
                Case "Maize"
                PictureBox1.Ima ge = My.Resources.ma ize
                Case "Fine"
                PictureBox1.Ima ge = My.Resources.su gar
                Case "SonaMahsur i"
                PictureBox1.Ima ge = My.Resources.Ri ce
                Case "Orange"
                PictureBox1.Ima ge = My.Resources.or ange
                Case "Bajra"
                PictureBox1.Ima ge = My.Resources.ba jra
                End Select
                i = i + 1
                MsgBox("s")
                'i want this msgbox to disappear as soon as it appears or make it invisible.

                End If
                End While
                End Sub

                Comment

                • Robbie
                  New Member
                  • Mar 2007
                  • 180

                  #9
                  Originally posted by vidyareddy
                  sir,
                  i have included the code in timer_tick event.
                  It is working fine only when i include a msgbox.otherwis e,its taking 100%cpu usage again.
                  I am able to see the results after every 5sec using timer but only problem being msgbox appearing over and over.
                  Is there anyway that this messagebox disappear automatically?
                  Could you not replace the message box with Application.DoE vents() ?

                  Comment

                  • vidyareddy
                    New Member
                    • Sep 2007
                    • 8

                    #10
                    Sir,
                    I tried using Application.Doe vents()
                    But it is taking 100% cpu usage and my exe running is getting hanged.Is there any other way of solving this problem?please reply.

                    Comment

                    • hariharanmca
                      Top Contributor
                      • Dec 2006
                      • 1977

                      #11
                      Originally posted by vidyareddy
                      Sir,
                      I tried using Application.Doe vents()
                      But it is taking 100% cpu usage and my exe running is getting hanged.Is there any other way of solving this problem?please reply.

                      Remove that while loop and if condition

                      Try those codes in Timer1_Timer event

                      [CODE=vb]private Sub Form_Load()
                      Timer1.Intervel = 100
                      end sub[/CODE]

                      Comment

                      • vidyareddy
                        New Member
                        • Sep 2007
                        • 8

                        #12
                        Originally posted by hariharanmca
                        Remove that while loop and if condition

                        Try those codes in Timer1_Timer event

                        [CODE=vb]private Sub Form_Load()
                        Timer1.Intervel = 100
                        end sub[/CODE]
                        srry sir,i didnt understand.Can u please explain me more clearly?i am new to VB.NET and am using it for the first time.please help me out.

                        Comment

                        • hariharanmca
                          Top Contributor
                          • Dec 2006
                          • 1977

                          #13
                          I thought VB 6.0.

                          in .NET also Remove that while loop and if condition in Timer1_Tick.

                          (I may be wrong, But you can try).

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Hi.

                            I just wanted to point out, 100% showing on the CPU graph is not always as bad as it sounds.

                            For example, this VB6 code...[CODE=vb]Do
                            Doevents
                            Loop[/CODE]will push the CPU to 100%, but it won't slow things down - or at least not much. Because all it's doing is continually telling Windows to go and check whether anyone else wants to use the CPU.

                            As a technique for making your program await something, this is obviously less than ideal. I'm just pointing out that it's not disastrous. A lot of people panic when they see the CPU meter hit the stops, but you have to consider the circumstances.

                            Comment

                            • vidyareddy
                              New Member
                              • Sep 2007
                              • 8

                              #15
                              Sir,
                              But the original exe which produces the results to be displayed on GUI is hanging.Otherwi se,there shud be no reason to complain about 100% cpu usage.
                              Originally posted by Killer42
                              I just wanted to point out, 100% showing on the CPU graph is not always as bad as it sounds ... consider the circumstances.
                              Last edited by Killer42; Sep 5 '07, 12:25 AM. Reason: Shortened excessive quote block

                              Comment

                              Working...