473,782 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4149
On Sun, 28 Dec 2003 19:44:38 -0800, "bill salkin"
<an*******@disc ussions.microso ft.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.Threadin g.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.DoE vents() call into the body of the loop.

"bill salkin" <an*******@disc ussions.microso ft.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*******@disc ussions.microso ft.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.Threadin g.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.DoE vents() 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.threa d.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*******@disc ussions.microso ft.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.CurrentT hread.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.EventArg s) Handles btnCancel.Click

mbStop = True

End Sub

To start thread

Dim trdMyLongProces s As New Threading.Threa d(AddressOf Me.MyLongProces s)

mbStop = False

trdMyLongProces s.Start()

Ken
-----------------
"bill salkin" <an*******@disc ussions.microso ft.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.DoE vents() 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*******@disc ussions.microso ft.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.CurrentT hread.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1575
by: | last post by:
newbie code -------------------------- #include <iostream> using namespace std; #include <cstring> class aaa {
6
6022
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 cannot seem to get my table to render properly, so I pulled out all the server controls and just made a very simple HTML only page and am having the same problem still. The problem can bee seen in the code below. The top row is 100px high and the...
1
1884
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 height? Width is ok, but height is clearly wrong. This looks very much like a bug to me. Further, when you try to check Dim mybool as Boolean = Dimensions.Contains(50.0,50.0)
18
2107
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
2143
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 user's viewable area in their browser. I'm close... really close, but I'm confused over how to mix specific measurements with percentages. When I began, things were quite nice: (http://play.psmonopoly.com/autosize.html). A little clunky in...
0
2161
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. i want to have header on top and footer on bottom. content should be on top of the middle part. i always got scrollbars, even if my page isn't too large. perhaps, someone can show me to do it right. the following code describes my wish. ...
6
5811
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 bytes of the file and some of these files are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the largest files and the other files take "forever" to get the last 100 bytes. This is the code I have currently that works with...
1
10842
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 pixel number, i.e. 800, will the gridview be 800 px wide. Here is an example: http://www.valuetronics.com/Results.aspx?keywords=8050&x=0&y=0 And this may be related, but this page
8
1717
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 wide) and add only the unique keys to a List<T>, Dictionary, Array... whatever is fastest because I'll be doing trillions of compares. The key is: ushort myBigKey = new ushort; I want to treat the entire array (all 100 instances) as one long...
1
2468
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
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.