1

I need to assign a higher task priority to a Parallel.Async background task. Since the OmniThreadLibrary has SetPriority: How can I set a specific priority to this Parallel.Async task?

uses
  CodeSiteLogging,
  OtlParallel, OtlTaskControl, OtlTask;

procedure TForm2.btnParallelAsyncClick(Sender: TObject);
begin
  CodeSite.Send('btnParallelAsyncClick 1');

  Parallel.Async(
    procedure(const task: IOmniTask)
    var
      a: Integer;
    begin
      // executed in background thread:
      a := 1 + 1;
      Sleep(2000);
      CodeSite.Send('Executed in Async Thread', a);

      task.Invoke( // used to execute code in main thread
        procedure
        begin
          CodeSite.Send('task.Invoke executed in main thread', a);
        end);

      Sleep(2000);
      CodeSite.Send('Again executed in Async Thread', a);
    end,
    Parallel.TaskConfig.OnTerminated(
    procedure(const task: IOmniTaskControl)
    begin
      // executed in main thread:
      CodeSite.Send('After background thread termination: Executed in Main Thread');
    end
    )
    );

  CodeSite.Send('btnParallelAsyncClick 2');
end;
user1580348
  • 5,721
  • 4
  • 43
  • 105

1 Answers1

1

Replace

Parallel.TaskConfig.OnTerminated(...

with for example

Parallel.TaskConfig.SetPriority(tpAboveNormal).OnTerminated(...

Possible values for the priority are

tpIdle, tpLowest, tpBelowNormal, tpNormal, tpAboveNormal, tpHighest

Please be aware that this only affects the priority given to the threads within your process and does not give the process itself a higher priority. See the documentation for the SetThreadPriority function for more info.

Olaf Hess
  • 1,453
  • 11
  • 18
  • Thank you very much, Olaf. However, when using `tpHighest`, the correct namespace must be specified: `Parallel.TaskConfig.SetPriority(TOTLThreadPriority.tpHighest).OnTerminated(...`. That's because the class `TThreadPriority` comes in: `E2010 Incompatible types: 'TOTLThreadPriority' and 'TThreadPriority'`. – user1580348 Dec 31 '16 at 18:29
  • Does this mean that the code in the `Parallel.Async` section will be executed faster with `tpHighest` than with `tpNormal`? – user1580348 Dec 31 '16 at 18:36
  • If I understand the documentation correctly the code in the `Parallel.Async` section will get more CPU time than the other threads in this process. If the other threads do nothing I am not sure whether there will be a noticeable effect. To change the priority of a whole process either do this via an API call ([How to set the process priority in C++](http://stackoverflow.com/questions/5216347/how-to-set-the-process-priority-in-c)) or use the "Change priority" menu in Windows Task Manager or start the program using the "start" command line command and pass the priority as a parameter. – Olaf Hess Jan 01 '17 at 09:32
  • @user1580348 It seems you're falling into the trap of a common misconception about multithreaded development. It's important to understand that: _Threads **do not** make code run faster - ever!_ They only make it _possible_ for code to run concurrently. If you are doing multiple things, you'll be able to do them in parallel. Each task will still require the same amount of processing time and resources. Increasing one thread's priority means that it _might_ (if not blocked by something else) get more processor time when many threads are competing for CPU. ***That's the best you can hope for***. – Disillusioned Jan 01 '17 at 12:35