473,398 Members | 2,404 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,398 software developers and data experts.

yielding cpu

I am writing a process that is a tight loop that consumes alot of cpu. Back in my VB days I could use "DoEvents" to periodically yield control to the OS incase there were any pending events. This would keep the app from being able to effectively take over the system. What is the equivilant in C#?
Nov 16 '05 #1
4 3021

You can use System.Threading.Thread.Sleep(0) to yield. If you actually
want to pump messages (which is what VB's DoEvents does), you use
System.Windows.Forms.Application.DoEvents().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Mattias is right. Application.DoEvents() does not block the process, it
pushes the messages on the message loop through, so that it appears to the
user that the gui is still responsive. But the other processes on the
machine will still be CPU starved, if this is what you are trying to avoid.

You may want to investigate using another thread instead of using
Application.DoEvents(). Also, be aware of what App.DoEvents really does. It
stops your process wherever it is and handles all windows messages. So
imagine the scenario where the user clicks a button, some heavy work hits,
and the gui goes non responsive. The user starts clicking around to see if
it's dead. Then you call App.DoEvents, and your intensive process is paused
while all of their clicks are processed. Now imagine that one of those
clicks they made was on the button that starts the heavy work. Now you're
running two copies of the heavy code, one execution occurring inside the
other.

Another scenario - imagine a database bound form with a save and close
button. The user clicks save, then while that's going, clicks cancel, and
you call App.DoEvents, the cancel event goes through and you close the form.
Then the save finishes and you go back to update the state of the form...
App.DoEvents should be used with caution.

Niall

"george" <ge************@kingland.com> wrote in message
news:E6**********************************@microsof t.com...
I am writing a process that is a tight loop that consumes alot of cpu.

Back in my VB days I could use "DoEvents" to periodically yield control to
the OS incase there were any pending events. This would keep the app from
being able to effectively take over the system. What is the equivilant in
C#?
Nov 16 '05 #3
Hello

You can also run the loop in a low priority thread.
Best regards,
Sherif
"george" <ge************@kingland.com> wrote in message
news:E6**********************************@microsof t.com...
I am writing a process that is a tight loop that consumes alot of cpu.

Back in my VB days I could use "DoEvents" to periodically yield control to
the OS incase there were any pending events. This would keep the app from
being able to effectively take over the system. What is the equivilant in
C#?
Nov 16 '05 #4
KJ
To yield CPU time, use Thread.Sleep

To do the repaint (which doesn't work very well) try Control.Refresh

To do things right, run the tight loop on a thread other than the main
"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Hello

You can also run the loop in a low priority thread.
Best regards,
Sherif
"george" <ge************@kingland.com> wrote in message
news:E6**********************************@microsof t.com...
I am writing a process that is a tight loop that consumes alot of cpu.

Back in my VB days I could use "DoEvents" to periodically yield control to
the OS incase there were any pending events. This would keep the app from
being able to effectively take over the system. What is the equivilant in
C#?

Nov 16 '05 #5

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

Similar topics

3
by: km | last post by:
Hi all, i didnt understand the purpose of 'yield' keyword and the concept of 'generators' in python. can someone explain me with a small example how generators differ from normal function...
3
by: Calvin Spealman | last post by:
I was wondering if it was possible, now or with a patch, to do either of the following: 1) Cause another thread's most recent function, or a given function in a given thread, to yield its...
2
by: One's Too Many | last post by:
Ran into a strange problem today: 8.1.7 on AIX 4.3.3 Database and applications had been working fine for two years and all of a sudden a couple of regularly-run queries are now no longer...
5
by: Stef | last post by:
Hello I have been programming C now for a couple of months but at this moment I am a bit confused with types like double float etc.. My question wat type does the result have when a double...
3
by: Kevin Chandler | last post by:
Greetings to All, I have a c# conversion app that does a lot of crunching and updates a status window containing a RichText control. The updates to the control do not appear until the job is...
7
by: (Jamie Andrews) | last post by:
For a research project, we're looking for a reliable parser for C that will take an ANSI C program and yield a tree representation of the program (as a Java or C++ object). Of course a grammar...
4
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: ...
1
by: abolduc | last post by:
I have had an underwhelming amount of success hunting down the source of this error and am hoping that someone here may have some insight. Error logged in the event log: Source: MSSQLSERVER...
1
by: Troels Arvin | last post by:
Hello, I'm getting a large number of paragraphs like this in a db2diag.log: 2008-10-22-09.31.50.339542+120 I277626395A468 LEVEL: Warning PID : 1576968 TID : 1 ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.