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

100% CPU

P: n/a
The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?
TIA,

Bill

Nov 20 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Sun, 28 Dec 2003 19:44:38 -0800, "bill salkin"
<an*******@discussions.microsoft.com> wrote:
The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?
TIA,

Bill


Add this:

System.Threading.Thread.Sleep(0)

Specifying 0 for the sleep will cause the thread to suspend so that
other waiting threads can execute.

// CHRIS

Nov 20 '05 #2

P: n/a
Throw an Application.DoEvents() call into the body of the loop.

"bill salkin" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?
TIA,

Bill


Nov 20 '05 #3

P: n/a
On Sun, 28 Dec 2003 19:44:38 -0800, "bill salkin"
<an*******@discussions.microsoft.com> wrote:
The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?
TIA,

Bill


Thought I should add that if you're doing a loop that many times,
30 million, you should also limit the number of times you call Sleep.
The overhead involved will definitely add up.

Maybe something like this:

for i = 1 to 30000000
if i mod 5000 = 0 Then System.Threading.Thread.Sleep(0)
next i
// CHRIS

Nov 20 '05 #4

P: n/a
On Sun, 28 Dec 2003 20:49:06 -0800, "Tom Dacon" <To**@t-dacons.com>
wrote:
Throw an Application.DoEvents() call into the body of the loop.


Beware of using a DoEvents(). Calling this method can cause code to
be re-entered if a message raises an event.

// CHRIS

Nov 20 '05 #5

P: n/a
Cor
Hi Bill,

I never tried it, but did you tried the
threading.thread.priority = threadpriority.lowest

Will you give us a sign if it did help than we know it also?

Cor

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?

Nov 20 '05 #6

P: n/a
"bill salkin" <an*******@discussions.microsoft.com> wrote in message
How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?


That's the entire point of multi-threading -- the operating system will do
this for you. Simply keep the code you're currently using. Whenever another
application also needs CPU time, the operating system will split the
available time over the two processes.
If your application is running at 100% CPU time, it means that there's no
other application that requires processor time.

If you wish to run your code in a low-priority background thread, take a
look at the Thread.CurrentThread.Priority property.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
Nov 20 '05 #7

P: n/a
Hi,

If you place it in a thread the cpu usage will stay at 100%.
However the program will not lock up. You still can start other procedures
and handle events. The thread will only run when it has time. Here is an
example. I assume you have a form level variable mbStop and a cancel button
btnCancel.

Public Sub MyLongProcess()

For i As Long = 1 To 30000000

Me.Text = i.ToString

If mbStop Then Exit For

Next

MessageBox.Show("Done")

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click

mbStop = True

End Sub

To start thread

Dim trdMyLongProcess As New Threading.Thread(AddressOf Me.MyLongProcess)

mbStop = False

trdMyLongProcess.Start()

Ken
-----------------
"bill salkin" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?
TIA,

Bill


Nov 20 '05 #8

P: n/a
* Chris Morse <ch***@sorry.no.spam.com> scripsit:
Throw an Application.DoEvents() call into the body of the loop.


Beware of using a DoEvents(). Calling this method can cause code to
be re-entered if a message raises an event.


ACK. And it won't reduce CPU usage...

:-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #9

P: n/a
On Mon, 29 Dec 2003 10:12:43 +0100, "Pieter Philippaerts"
<Pi****@nospam.mentalis.org> wrote:
"bill salkin" <an*******@discussions.microsoft.com> wrote in message
How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?
That's the entire point of multi-threading -- the operating system will do
this for you. Simply keep the code you're currently using. Whenever another
application also needs CPU time, the operating system will split the
available time over the two processes.


Yes, in theory, but in practice this is sometimes not the case. I
have had "runaway processes" (like, once in a while, the File Transfer
window in pcAnywhere) that hog the CPU almost completely and make it
almost impossible to do anything else.
If your application is running at 100% CPU time, it means that there's no
other application that requires processor time.
Absolutely not. Dozens of processes are then competing for what
little CPU time that's left over. You should avoid 100% cpu
utilization if possible. Adding a sleep() call now and then can
drastically improve overall system performance. Likewise, as Cor
pointed out, you can run the thread at a lower priority. This will
help keep a CPU hog from (what appears to be) locking the system up;
though you'll still have 100% CPU utilization; just that other
processes will get more time to do their thing.
If you wish to run your code in a low-priority background thread, take a
look at the Thread.CurrentThread.Priority property.
That's a good idea.
Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl


// CHRIS

Nov 20 '05 #10

P: n/a
Cor,

Here are my results so far:
1. The statement

If i Mod 2 = 5000 Then
System.Threading.Thread.Sleep(0)
End if

has almost no impact on CPU utilization (as viewed through
Win 2K Task Manager). The same seems to be true for setting

2.
Threading.Thread.CurrentThread.Priority =
Threading.ThreadPriority.Lowest

I find in both cases that the app still hogs around 97%
of the CPU but suprisingly the loop completion time
increases only slightly in either case (a fraction of a
second for 300,000 iterations)

Bill
-----Original Message-----
Hi Bill,

I never tried it, but did you tried the
threading.thread.priority = threadpriority.lowest

Will you give us a sign if it did help than we know it also?
Cor

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?
.



-----Original Message-----
Hi Bill,

I never tried it, but did you tried the
threading.thread.priority = threadpriority.lowest

Will you give us a sign if it did help than we know it also?
Cor

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?

.

Nov 20 '05 #11

P: n/a
On Mon, 29 Dec 2003 13:32:00 -0700, "Dick Grier"
<di**************@msn.com> wrote:

(SNIP)
Application.DoEvents DOES NOT issue a call to Sleep. (SNIP)Dick


Sorry- Apparently my bad.

My statement is TRUE for VB 4.0/5.0/6.0, though. That is, if you
believe the MSDN documentation (see "HOWTO: Determine the Differences
Between DoEvents and Sleep", Q158175.)

The .NET documentation does not say anything about it either way (that
I can find.)

// CHRIS

Nov 20 '05 #12

P: n/a
On Mon, 29 Dec 2003 16:46:40 -0800, "bill salkin"
<an*******@discussions.microsoft.com> wrote:
I find in both cases that the app still hogs around 97%
of the CPU but suprisingly the loop completion time
increases only slightly in either case (a fraction of a
second for 300,000 iterations)

Bill


Yes, I thought you were trying to avoid the "lock up" problem, where
other windows stop responding (or do so VERY slowly.)

If you want to decrease your CPU utilization, you'll have to put in
positive sleep values (try different values for the MOD value and the
Sleep(n) value).

The more you sleep, the less the CPU usage, and the longer it will
take for the loop to complete.

For k = 1 To 30000000
If k Mod 5000 = 0 Then
System.Threading.Thread.Sleep(1)
End If
Next k

The value of 5000 is arbitrary. The more "work" you're doing in your
loop, the lower the value you'll want to use. Otherwise, it will make
little or no difference at all.

Play around with the numbers and get a feel for what combinations do.
Doing a Sleep(1) means (ideally) a 1 millisecond sleep. So that means
you can only do that 1000 times a second.

Since the k Mod 5000 allows it to go every 5000 itterations, your loop
will have an overhead the following overhead:

30 million itterations
times
1 millisecond sleep / 5,000 itterations

= 6,000 milliseconds sleep

= 6 seconds

You have to ask yourself how long you're willing to let the loop take
-vs- how much CPU usage you want to let it use.

// CHRIS

Nov 20 '05 #13

P: n/a
Cor
Hi Bill,

That the application uses the CPU normaly at about 97% is in my opinion
normal, I have that with every application.

However now you are testing, why do you not write two test programs, one
with the thread.sleep and one with the thread.priority lowest.

Start them at the same time and see the result.

I am curious for your answer.

Cor

1. The statement

If i Mod 2 = 5000 Then
System.Threading.Thread.Sleep(0)
End if

has almost no impact on CPU utilization (as viewed through
Win 2K Task Manager). The same seems to be true for setting

2.
Threading.Thread.CurrentThread.Priority =
Threading.ThreadPriority.Lowest

I find in both cases that the app still hogs around 97%
of the CPU but suprisingly the loop completion time
increases only slightly in either case (a fraction of a
second for 300,000 iterations)

Bill

Nov 20 '05 #14

P: n/a
One problem, the load will be different depending on CPU type/speed.

How do you work arround that?
Maybe check spu load?

Schneider

"bill salkin" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
Cor,

Here are my results so far:
1. The statement

If i Mod 2 = 5000 Then
System.Threading.Thread.Sleep(0)
End if

has almost no impact on CPU utilization (as viewed through
Win 2K Task Manager). The same seems to be true for setting

2.
Threading.Thread.CurrentThread.Priority =
Threading.ThreadPriority.Lowest

I find in both cases that the app still hogs around 97%
of the CPU but suprisingly the loop completion time
increases only slightly in either case (a fraction of a
second for 300,000 iterations)

Bill
-----Original Message-----
Hi Bill,

I never tried it, but did you tried the
threading.thread.priority = threadpriority.lowest

Will you give us a sign if it did help than we know it

also?

Cor

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?

.



-----Original Message-----
Hi Bill,

I never tried it, but did you tried the
threading.thread.priority = threadpriority.lowest

Will you give us a sign if it did help than we know it

also?

Cor

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?

.

Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.