-1

How can I run tasks without TOmniEventMonitor? If I start them without it the main thread freezes. Which makes no sense because OmniThreadLibrary is supposed to be based on TThread. UnObserved doesn't really fix this because it just makes an internal copy of the same thing.

type
  TWorker = class(TOmniWorker)
    function Initialize: Boolean; override;
    constructor Create;
  end;

begin
var
  Task: IOmniTaskControl;
begin
  Task := CreateTask(TWorker.Create()).Run; // blocks main thread
  Task := CreateTask(TWorker.Create()).UnObserved.Run; // will create internal monitor each time
  Task := CreateTask(TWorker.Create()).OnTerminated().Run; // will create internal monitor each time
end.

If I create a TThread it doesn't need any kind of "Monitors" and it doesn't block the main thread either. I am not sending any kind of messages so why the need for a "Monitor"?

  • Some code would help here... – Uwe Raabe Jul 22 '14 at 07:00
  • If I am understanding your question correctly, you don't need a monitor. You only need a monitor if you don't save the result of CreateTask in a variable which returns an IOmniTask... I don't have Delphi open so I am typing this from memory. – Graymatter Jul 22 '14 at 07:14
  • 2
    Please read: http://www.thedelphigeek.com/2009/11/omnithreadlibrary-patterns-task.html – gabr Jul 22 '14 at 07:20
  • @gabr The solution was `List: TList` equally `Array` would work aswell. – user3863403 Jul 22 '14 at 07:54

1 Answers1

2

You are doing exactly that thing mentioned chapter 4.4 in the book Parallel Programming with OmniThreadLibrary as

The simplest example of the wrong approach can be written in one line:

CreateTask(MyWorker).Run;

As a solution you can assign the result of CreateTask to a variable with a scope that covers the runtime of the process.

The other solution (as you found yourself) is to use a monitor.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130