run a for loop wihout take up all pc resources??????

ksboyz
ksboyz used Ask the Experts™
on
i have encountered some problems when running a infinite loop .....the program almost take up alll the pc resource.......can i make the program run at lower level priority.......does it really work for non realtime OS like winXP????


from
ksboyz
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You should avoid infinite loops.
Could you post some code? can't you wait until a certain event occurs (and let the OS do the waiting).
Or you could place a sleep(millisecs) in the loop in order to give the OS time to do other stuff.

JOHan.

Commented:
in Windows you can use timers
SetTimer()
KillTimer()
process in WM_TIMER message or a callback function


without timer's (threads)
theres an API call SetThreadPriority()
that you can call on the thread handle

for MFC
CWinThread has a method
CWinThread::SetThreadPriority()

check them out in MSDN...
no sure how effective...
sorta depends on what your doing...
some tasks in loops don't time share well in either threads or loops due to system resource calls.

btw, my experience setting thread priority to THREAD_PRIORITY_HIGHEST will essentially disable so that no other program get enough time shares to run even slightly effectively


viel Glueck!
jkr
Top Expert 2012

Commented:
>>does it really work for non realtime OS like winXP????

As johanz already mentioned, you would yield processor time by calling 'Sleep()' or 'SleepEx()'. See also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/suspending_thread_execution.asp ("Suspending Thread Execution")

>>can i make the program run at lower level priority

This is also an option:

SetThreadPriority ( GetCurrentThread (), THREAD_PRIORITY_BELOW_NORMAL);

For a list of all priority levels, see http://msdn.microsoft.com/library/en-us/dllproc/base/setthreadpriority.asp ("SetThreadPriority")
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to johanhz  

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial