6

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:

Screenshot of 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 :)

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Nevermind I found the answer. Typical, I tried finding the answer for ages and as soon as I asked I found the solution! Will Edit my question to reflect the changes. –  Jan 30 '12 at 16:53
  • I could not edit anyway. I added a comment to your answer it shows the link where I found the answer. –  Jan 30 '12 at 16:59

2 Answers2

6

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.

const
  BELOW_NORMAL_PRIORITY_CLASS = $00004000
  ABOVE_NORMAL_PRIORITY_CLASS = $00008000

As an aside remember that CreateProcess modifies its second parameter, lpCommandLine, the parameter to which you pass PChar(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 line

UniqueString(FileName);

immediately before the call to CreateProcess. More information can be found here: Access Violation in function CreateProcess in Delphi 2009

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I just found this out from here after Google search: http://upxshell.googlecode.com/svn-history/r3/trunk/Source/MainSource/Shared.pas Why is this not included in Windows.pas though?? –  Jan 30 '12 at 16:57
  • Thanks for the extra information, I will make sure I take more care with CreateProcess. –  Jan 30 '12 at 17:01
  • @Blobby Not sure. My guess is that Windows.pas was originally written to support versions of Windows which did not have these priority classes. Later when they were added to Windows, the Delphi header did not keep pace. – David Heffernan Jan 30 '12 at 17:01
  • I guess that makes sense, in the link I posted it notes the OS compatibility for those 2 priorities. But I am using Delphi XE so you would expect this to be included/updated now. Is this the same for Delphi XE-2? Embarcadero should really update some of these out of date functions and things :) Because those are not included, I believed it was not possible unless using some other code method. –  Jan 30 '12 at 17:10
  • It's the same in XE2. I agree that it's an omission. That said, Emba have done a lot of work improving and broadening the header translations, but I guess there is more to do. If ever you hit a block on a Windows API, always look it up in MSDN. You will always be able to gain access to new Windows functionality even if the Delphi header translation is incomplete. – David Heffernan Jan 30 '12 at 17:13
  • Will do thanks, and also thanks for mentioning UniqueString. I was not familiar with it, so I will read some information on that too. –  Jan 30 '12 at 17:15
-2
IDLE_PRIORITY_CLASS
BELOW_NORMAL_PRIORITY_CLASS
NORMAL_PRIORITY_CLASS
ABOVE_NORMAL_PRIORITY_CLASS
HIGH_PRIORITY_CLASS
REALTIME_PRIORITY_CLASS

Reference

Abdul Hfuda
  • 1,463
  • 12
  • 25