Task Configuration

High-level abstractions will do most of the hard work for you, but sometimes you'll still have to apply some configuration to the low-level parallel tasks (entities represented with the IOmniTask interface). The mechanism for doing low-level configuration is called task configuration block.

Task configuration block, or IOmniTaskConfig, is an interface returned from the Parallel.TaskConfig factory function.

class function Parallel.TaskConfig: IOmniTaskConfig;
begin
  Result := TOmniTaskConfig.Create;
end;

This interface contains various functions that set up messaging handlers, termination handlers and so on. All function return interface itself so they can be used in a fluent manner.

IOmniTaskConfig = interface
  procedure Apply(const task: IOmniTaskControl);
  function  CancelWith(
    const token: IOmniCancellationToken): IOmniTaskConfig;
  function  MonitorWith(
    const monitor: IOmniTaskControlMonitor): IOmniTaskConfig;
  function  OnMessage(
    eventDispatcher: TObject): IOmniTaskConfig; overload;
  function  OnMessage(
    eventHandler: TOmniTaskMessageEvent): IOmniTaskConfig; overload;
  function  OnMessage(
    msgID: word; 
    eventHandler: TOmniTaskMessageEvent): IOmniTaskConfig; overload;
  function  OnMessage(
    msgID: word; 
    eventHandler: TOmniOnMessageFunction): IOmniTaskConfig; overload;
  function  OnTerminated(
    eventHandler: TOmniTaskTerminatedEvent): IOmniTaskConfig; overload;
  function  OnTerminated(
    eventHandler: TOmniOnTerminatedFunction): IOmniTaskConfig; overload;
  function  OnTerminated(
    eventHandler: TOmniOnTerminatedFunctionSimple): IOmniTaskConfig; 
    overload;
  function  WithCounter(
    const counter: IOmniCounter): IOmniTaskConfig;
  function  WithLock(
    const lock: TSynchroObject; 
    autoDestroyLock: boolean = true): IOmniTaskConfig; overload;
  function  WithLock(
    const lock: IOmniCriticalSection): IOmniTaskConfig; overload;
end; 

- Apply applies task configuration block to a task. It is used internally in the OtlParallel unit.

- CancelWith shares cancellation token with the task.

- MonitorWith attaches task to the monitor.

- OnMessage handlers set up message dispatch to an object (for example, a form) or to an event handler. In the latter case, event handler can be set for specific message ID or for all messages.

- OnTerminated sets up termination handler which is called when the task terminates.

- WithCounter shares a counter with the task.

- WithLock shares a lock object with the task.

Examples of task configuration block usage are demonstrated in demo 47_TaskConfig. Some examples are also given in sections describing individual abstractions.

book/highlevel/taskconfig.txt · Last modified: 2012/11/14 11:54 by gabr
Recent changes RSS feed Debian Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki