News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Windows friendly

Started by TNick, July 12, 2006, 09:49:12 AM

Previous topic - Next topic

TNick

How to make a program Windows friendly? (let other programs run without slowing them down too much) Is there a Windows function or some tric?

Tedd

It's generally a default :P You have to TRY to make it so that other windows are slowed down (supposedly, at least ::))
Each window (technically: each process) gets a certain amount of cpu time before the next one takes its turn. You can, however, change a thread's priority to make it get a little more (or a little less) cpu time -- SetThreadPriority. So, if you want to do something 'in the background' without taking (much) time away from other programs, you can set your priority to THREAD_PRIORITY_IDLE or THREAD_PRIORITY_LOWEST (depending how much time you want to give up.)
No snowflake in an avalanche feels responsible.

TNick

Right, right, I forget about that. Thanks, Tedd!

hutch--

Nick,

You really don't have to worry about it on a 32 bit system as it is taken care of for you in the message loop. GetMessage() does not return unless there is a message waiting to be processed so while the program is doing nothing, it uses no processor time. When a message becomes available, it is then passed to the app in the normal manner and the app handles the ,message in whatever way it likes. Note also that many messages are handled by the default handler in a WndProc or similar so you don't specifically have to respond to them.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

TNick

Yes, hutch, thanks!
But if your program executes a operation that need a serious amount of time to be completed and that operation need a lot of processor time, other process that run on that computer respond slow to user input. Anyway, I think THREAD_PRIORITY_IDLE or THREAD_PRIORITY_LOWEST are the answer to my problem.

dsouza123

If the issue is your program is going to do alot of calculations and either doesn't process messages or
putting the calculation in the WndProc would stop it from responding, then use a worker thread.

There is an example program, Iczelion tutorial 16 ( thread2.asm ) that comes with Masm32, which does this.
This way messages get processed, no issue with reponsiveness, and the calculation(s) get done.

With one extra API call, the worker thread can be set to THREAD_PRIORITY_IDLE for the minimal impact on the system.
after
  mov  hThread,eax
add
  invoke SetThreadPriority,hThread,THREAD_PRIORITY_IDLE

TNick

dsouza123,

Thanks for your post.
I understand what you are sayng. Let's try with an example (not real):
- we have a program that copies files from a DVD to a hard-disk and scans the bytes for something; the user has started a copy operation
- "Oh," he say, "this will be a long one ..." :(
- the user is bored and he/she want to see a movie
- he/she choose "The Shawshank Redemption", open this movie in a player and watch the movie
- the user has a 486

.
.
.


Now: what can do the developer of that program that copies files in order to let the player work OK?

This is my problem and, as I was saing, the answer I think lies in Tedd's post.

zooba

The easiest way (without having read the other replies in-depth) is to put an invoke Sleep, 0 call in reasonable places (maybe second or third innermost loop, not the innermost one). It will slow the calculation down massively, but Windows will remain perfectly responsive. In most cases where I've used it, CPU usage has gone from 99% to 0-1% and made very little noticable difference to the time it takes to complete.

If the loop is really tight the OS won't break into it very well. Putting invoke Sleep, 0 lets the OS pass the CPU to any other processes who want it (don't worry, you'll get it back :wink )

Cheers,

Zooba :U

TNick

Thanks, zooba, that would be it! :D