-
August 6th, 2013, 03:50 AM
#1
My aplication uses 100% of a CPU core even if idle.
I'm developig a simple aplication with vb.net on visual studio 2012 but it's very CPU consuming, i'm running it on a I5 with 4 cores and always cosumes exactly 25% of the CPU, one full core, even if idle
Because other programs don't do that kind of suff i'ts obvious that i'm doing something wrong, here is the code of the program:
This is the main form load, it starts with two methods that read a bunch of files and shows a listview, then it goes to the main waiting proc:
Code:
Private Sub PTX_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Show()
generate_site_list()
show_site_list()
main_proc()
End Sub
And this is the waiting mode, it waits for the user to hit some buttons that executes various operations, since the program is intended to use various threads at the same time I need to monitor witch ones are finished, but for now i've coded that simple function:
Code:
Private Sub main_proc()
While (1)
Application.DoEvents()
End While
End Sub
now, ive tried to write this one down as:
Code:
Private Sub main_proc()
While (1)
Threading.Thread.Sleep(0)
End While
End Sub
Or even to not call it at all comenting it at the form load, but the CPU is still at 25%, and if i load another thread, it also takes over another full core,
As I come from mainly coding with C, i'm pretty nobbie with vb.net, what i'm missing here?
-
August 6th, 2013, 12:27 PM
#2
Re: My aplication uses 100% of a CPU core even if idle.
Those loops you are using are called spin-waits and they are strongly discouraged for the very reason you are concerned about. Essentially they consume a hell of a lot of CPU cycles while not doing anything useful. Avoid them at all costs.
-
August 7th, 2013, 10:36 AM
#3
Re: My aplication uses 100% of a CPU core even if idle.
You may want to have a look at the BackgroundWorker class and avoid DoEvents
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|