如何为 Parallel.Async 后台任务设置更高的任务优先级?
How to set a higher task priority to a Parallel.Async background task?
我需要为 Parallel.Async
后台任务分配更高的任务优先级。由于 OmniThreadLibrary 有 SetPriority
:我如何为这个 Parallel.Async
任务设置特定的优先级?
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;
替换
Parallel.TaskConfig.OnTerminated(...
例如
Parallel.TaskConfig.SetPriority(tpAboveNormal).OnTerminated(...
优先级的可能值为
tpIdle, tpLowest, tpBelowNormal, tpNormal, tpAboveNormal, tpHighest
请注意,这只会影响您进程中线程的优先级,不会给进程本身更高的优先级。有关详细信息,请参阅 SetThreadPriority 函数的文档。
我需要为 Parallel.Async
后台任务分配更高的任务优先级。由于 OmniThreadLibrary 有 SetPriority
:我如何为这个 Parallel.Async
任务设置特定的优先级?
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;
替换
Parallel.TaskConfig.OnTerminated(...
例如
Parallel.TaskConfig.SetPriority(tpAboveNormal).OnTerminated(...
优先级的可能值为
tpIdle, tpLowest, tpBelowNormal, tpNormal, tpAboveNormal, tpHighest
请注意,这只会影响您进程中线程的优先级,不会给进程本身更高的优先级。有关详细信息,请参阅 SetThreadPriority 函数的文档。