How can I create and run a process from my program with the ability to set the priority of the process?
This is what I have so far:
const
LOW_PRIORITY = IDLE_PRIORITY_CLASS;
//BELOW_NORMAL_PRIORITY = ???
NORMAL_PRIORITY = NORMAL_PRIORITY_CLASS;
//ABOVE_NORMAL_PRIROTY = ???
HIGH_PRIORITY = HIGH_PRIORITY_CLASS;
REALTIME_PRIORITY = REALTIME_PRIORITY_CLASS;
procedure RunProcess(FileName: string; Priority: Integer);
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
Done: Boolean;
begin
FillChar(StartInfo,SizeOf(TStartupInfo),#0);
FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
StartInfo.cb := SizeOf(TStartupInfo);
try
Done := CreateProcess(nil, PChar(FileName), nil, nil,False,
CREATE_NEW_PROCESS_GROUP + Priority,
nil, nil, StartInfo, ProcInfo);
if not Done then
MessageDlg('Could not run ' + FileName, mtError, [mbOk], 0);
finally
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;
The above code works in that I can set the priority of the executed process. But see the image below of the Windows Task Manager:

You can set more options such as Below Normal and Above Normal which is what I want to also be able to set. Looking through Windows.pas I see no such value.
How can I create and run my process with those extra parameters?
Thanks 🙂
Those two flags are not declared in the Windows.pas that ships with Delphi. You will have to declare these values for yourself. The values can be found in the MSDN documentation of SetPriorityClass.
As an aside remember that
CreateProcessmodifies its second parameter,lpCommandLine, the parameter to which you passPChar(FileName). So your code will fail if you call the function passing a string literal which lives in read-only memory. I would add the following lineimmediately before the call to CreateProcess. More information can be found here: Access Violation in function CreateProcess in Delphi 2009