Microsoft KB Archive/193846

= How to modify the process priority of a shelled application with Visual FoxPro =

Article ID: 193846

Article Last Modified on 1/27/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q193846



SUMMARY
A process' priority determines how its execution is scheduled by the Win32 kernel. Processes with a higher priority execute more quickly than processes with a lower priority. This article describes how to modify the Process priority of a shelled application.



MORE INFORMATION
Win32 supports the following four priority classes:
 * Idle
 * Normal
 * High
 * Realtime

Idle is the correct priority for system-monitoring applications or screen savers.

Normal priority class is the default, indicating a process with no special scheduling needs.

High priority indicates a process that performs time-critical tasks requiring immediate execution for it to run correctly. The threads of a high-priority class process preempt the threads of normal or idle priority class processes. An example is the Windows Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high- priority class CPU-bound application can use almost all available cycles.

The Realtime priority indicates a process that has the highest possible priority. The threads of a real-time priority class process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive. This priority should almost never be used.

The process' priority is set by assigning one of the parameters of the CreateProcess API call.

The following example demonstrates running the Windows NOTEPAD application under Windows NT 4.0 and later Windows operating systems using an idle priority. The process priority is controlled by the 'dwCreationFlags' variable passed to CreateProcess. DEFINEs for the four supported priority classes are listed at the top of the sample program.

Once Notepad.exe is running, to examine the priority, start the Windows Task Manager, select the Processes tab, right-click Notepad.exe and select 'Set Priority' from the pop-up menu.

The fully qualified pathname to Notepad.exe may need modification in the assignment statement of variable 'File2Run'.

Sample code
#DEFINE NORMAL_PRIORITY_CLASS 32 #DEFINE IDLE_PRIORITY_CLASS 64 #DEFINE HIGH_PRIORITY_CLASS 128 #DEFINE REALTIME_PRIORITY_CLASS 1600

DECLARE INTEGER CreateProcess IN kernel32.DLL ; INTEGER lpApplicationName, ; STRING lpCommandLine, ; INTEGER lpProcessAttributes, ; INTEGER lpThreadAttributes, ; INTEGER bInheritHandles, ; INTEGER dwCreationFlags, ; INTEGER lpEnvironment, ; INTEGER lpCurrentDirectory, ; STRING @lpStartupInfo, ; STRING @lpProcessInformation

DECLARE INTEGER GetLastError IN kernel32.DLL

* Startup info is 68 bytes, of which we need to  * initially populate the 'cb' or Count of Bytes member * with the overall length of the structure. * The remainder should be 0-filled. START = long2str(68) + REPLICATE(CHR(0), 64)

* PROCESS_INFORMATION structure is 4 longs, * or 4*4 bytes = 16 bytes, which we'll initially zero. process_info = REPLICATE(CHR(0), 16)

* Start a copy of NOTEPAD (EXE name must be null-terminated) File2Run = "C:\WINNT\NOTEPAD.EXE" + CHR(0)

* Call CreateProcess, obtain a process handle. Treat the * application to run as the 'command line' argument, accept * all other defaults. Important to pass the start and * process_info by reference. RetCode = CreateProcess(0, File2Run, 0, 0, 1, ;     IDLE_PRIORITY_CLASS, 0, 0, @START, @process_info)

* Unable to run, exit now. IF RetCode = 0 =MESSAGEBOX("Error occurred. Error code: ", GetLastError) RETURN ENDIF

********************  FUNCTION long2str ********************  * passed : 32-bit non-negative numeric value (m.longval) * returns : ascii character representation of passed *   value in low-high format (m.retstr) * example : *  m.long = 999999 *  m.longstr = long2str(m.long) PARAMETERS m.longval

PRIVATE i, m.retstr

m.retstr = "" FOR i = 24 TO 0 STEP -8 m.retstr = CHR(INT(m.longval/(2^i))) + m.retstr m.longval = MOD(m.longval, (2^i)) NEXT RETURN m.retstr

