By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,807 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,807 IT Pros & Developers. It's quick & easy.

How can i avoid 100% cpu usage?

P: 8
i have developed a speech processing exe (31aug.xe) which listen to what u speak and write it in a file called OUTPUT.According to the contents in file called CHECK,picture shud change.Display should change only when there is "b' in DISPLAYfile.otherwise,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

Expand|Select|Wrap|Line Numbers
  1. Shell("31aug.exe", vbNormalFocus)
  2.         'System.Threading.Thread.Sleep(5000)
  3. 'this waits till user speaks something.
  4.  
  5.         Dim iofile As New StreamReader("DISPLAY")
  6.         Dim ioline As String
  7.         ioline = iofile.ReadLine
  8.         iofile.Close()
  9.         Dim i As Integer
  10.         i = 0
  11.  
  12.         While i >= 0
  13.             If i > 0 Then
  14.                 Me.Update()
  15.             End If
  16.  
  17.             If ioline = "b" Then
  18.  
  19.                 Dim iofile1 As New StreamReader("OUTPUT")
  20.                 Dim ioline1 As String
  21.                 ioline1 = iofile1.ReadLine
  22.                 iofile1.Close()
  23.                  mainTB.Text = ioline1
  24.  
  25.                 Dim iofile2 As New StreamReader("CHECK")
  26.                 Dim ioline2 As String
  27.                 ioline2 = iofile2.ReadLine
  28.                 iofile2.Close()
  29.                 If ioline2 = "Dusheri" Then
  30.                     PictureBox1.Image = My.Resources.mango1
  31.                 Else
  32.                     If ioline2 = "Orange" Then
  33.                         PictureBox1.Image = My.Resources.orange
  34.                     Else
  35.                         If ioline2 = "Maize" Then
  36.                             PictureBox1.Image = My.Resources.maize
  37.                         Else
  38.                             If ioline2 = "SonaMahsuri" Then
  39.                                 PictureBox1.Image = My.Resources.Rice
  40.                             Else
  41.                                 If ioline2 = "Fine" Then
  42.                                     PictureBox1.Image = My.Resources.sugar
  43.                                 Else
  44.                                     If ioline2 = "Bajra" Then
  45.                                         PictureBox1.Image = My.Resources.bajra
  46.  
  47.                                     End If
  48.                                 End If
  49.                             End If
  50.                         End If
  51.  
  52.                     End If
  53.                 End If
  54.  
  55.                 i = i + 1
  56.                 'MsgBox("shall we go again?")
  57.             End If
  58.         End While
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.
Sep 3 '07 #1
Share this Question
Share on Google+
15 Replies


hariharanmca
100+
P: 1,977
Expand|Select|Wrap|Line Numbers
  1. While i >= 0
  2. If i > 0 Then
  3. Me.Update()
  4. End If
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
Sep 3 '07 #2

Robbie
100+
P: 180
If you're using VB6, all you need to do is add a line of code:
Expand|Select|Wrap|Line Numbers
  1. DoEvents
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:
Expand|Select|Wrap|Line Numbers
  1. if colour="Yellow" then
  2. MsgBox "It's yellow"
  3. else
  4. if colour="Red" then
  5. MsgBox "It's red"
  6. else
  7. if colour="Blue" then
  8. MsgBox "It's blue"
  9. else
  10. if colour="Green" then
  11. MsgBox "It's green"
  12. end if
  13. end if
  14. end if
  15. end if
  16.  
You could do this, which is much neater and much more easy-to-follow:
Expand|Select|Wrap|Line Numbers
  1. Select Case colour
  2.     case "Yellow"
  3.         MsgBox "It's yellow"
  4.     case "Red"
  5.         MsgBox "It's red"
  6.     Case "Blue"
  7.         MsgBox "It's blue"
  8.     Case "Green"
  9.         MsgBox "It's green"
  10. End Select
  11.  
Hope it helps! =D
Let me know how you get on!
Sep 3 '07 #3

P: 8
@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.
Sep 3 '07 #4

P: 8
@ robbie
sir,
thanks for select code.It makes code simpler.I am using VB.NET 2005 express edition.Please suggest me something.
Sep 3 '07 #5

hariharanmca
100+
P: 1,977
@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).
Sep 3 '07 #6

Robbie
100+
P: 180
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.DoEvents()
Hope it helps! =D
Sep 3 '07 #7

P: 8
sir,
i have included the code in timer_tick event.
It is working fine only when i include a msgbox.otherwise,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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Shell("31aug.exe", vbNormalFocus)
System.Threading.Thread.Sleep(4000)
'this allows time for level measurement
Timer1.Interval = 6000
'timer starts
Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer = 0
Dim iofile As New StreamReader("DISPLAY")
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("OUTPUT")
Dim ioline1 As String
ioline1 = iofile1.ReadLine
iofile1.Close()
mainTB.Text = ioline1
Dim iofile2 As New StreamReader("CHECK")
Dim ioline2 As String
ioline2 = iofile2.ReadLine
iofile2.Close()
Select Case ioline2
Case "Dusheri"
PictureBox1.Image = My.Resources.mango1
Case "Maize"
PictureBox1.Image = My.Resources.maize
Case "Fine"
PictureBox1.Image = My.Resources.sugar
Case "SonaMahsuri"
PictureBox1.Image = My.Resources.Rice
Case "Orange"
PictureBox1.Image = My.Resources.orange
Case "Bajra"
PictureBox1.Image = My.Resources.bajra
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
Sep 3 '07 #8

Robbie
100+
P: 180
sir,
i have included the code in timer_tick event.
It is working fine only when i include a msgbox.otherwise,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.DoEvents() ?
Sep 3 '07 #9

P: 8
Sir,
I tried using Application.Doevents()
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.
Sep 3 '07 #10

hariharanmca
100+
P: 1,977
Sir,
I tried using Application.Doevents()
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

Expand|Select|Wrap|Line Numbers
  1. private Sub Form_Load()
  2.      Timer1.Intervel = 100
  3. end sub
Sep 3 '07 #11

P: 8
Remove that while loop and if condition

Try those codes in Timer1_Timer event

Expand|Select|Wrap|Line Numbers
  1. private Sub Form_Load()
  2.      Timer1.Intervel = 100
  3. end sub
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.
Sep 3 '07 #12

hariharanmca
100+
P: 1,977
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).
Sep 4 '07 #13

Expert 5K+
P: 8,434
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...
Expand|Select|Wrap|Line Numbers
  1. Do
  2.   Doevents
  3. Loop
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.
Sep 4 '07 #14

P: 8
Sir,
But the original exe which produces the results to be displayed on GUI is hanging.Otherwise,there shud be no reason to complain about 100% cpu usage.
I just wanted to point out, 100% showing on the CPU graph is not always as bad as it sounds ... consider the circumstances.
Sep 4 '07 #15

P: 8
@all who replied

thanks a lot to everybody replied.i would not have finished my project so fast without your help.Once again thanks :D
Sep 4 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.