473,322 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

100% CPU

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
14 4111
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
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
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
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
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
"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
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
* 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
newbie code -------------------------- #include <iostream> using namespace std; #include <cstring> class aaa {
6
by: jslaybaugh | last post by:
I'm working on an ASP.NET 2.0 application and am having trouble with a very simple table layout. Using ASP.NET 2.0 it defaults to XHTML 1.0 Transitional and I am trying to comply. However, I...
1
by: Hypnotron | last post by:
Dim Dimensions as RectangleF = RectangleF.FromLTRB(-100, 100, 100, -100) debug.writeline( "Height = " & RectangleF.Height.ToString) Why the heck is height -200 ? How can you have a negative...
18
by: P.N. | last post by:
Hi! when i define array (any type) then compilator say that array is to large! ?? i need at least 10.000 instead 100. This is for numeric methods in "C" any sugestion? thx Greetings P
17
by: Mike | last post by:
I'm trying to create a page: Three sections (left, topright and bottomright), each with a heading and scrolling (overflow) content. The size of these sections should be based upon the size of the...
0
by: Markus Olderdissen | last post by:
i want to create my page with 100% height. <table height="100%"works but is not correct by default. i saw various information how to do it with stylesheet. i really have problems to create my page....
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
1
by: psion | last post by:
Hi, We have a gridview on a webpage, which we would like to be 100% of the table cell in which it is placed. When we specify the width to be 100%, this has no effect, but only if we specify a...
8
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
1
by: Meganutter | last post by:
Hello, i am having some troubles with DIVs. heres my setup i have a wrapper, 900px wide 100% height nested header 100% wide 20px high nested menubar 100%wide 80px high
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.