Question My aplication uses 100% of a CPU core even if idle.

black_tx

New member
Joined
Aug 6, 2013
Messages
1
Programming Experience
Beginner
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:

VB.NET:
   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:

VB.NET:
   Private Sub main_proc()
        While (1)
            Application.DoEvents()
        End While
    End Sub

now, ive tried to write this one down as:


VB.NET:
   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?
 
You need to put a delay inside your loop, otherwise it's 100% occupied, as expected. That is the concept of duty cycle. In your case you have a 100% duty cycle, where it should really be more around 1-2%. Add Thread.Sleep(200) inside your loop.
 
Back
Top