Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

IOmniTaskControl.Unobserved - the task is never destroyed

I am trying to run a fire-and-forget task - IOmniTaskControl.Unobserved works well enough with either IOmniTaskControl.Run or IOmniTaskControl.Schedule(IOmniThreadPool), but the IOmniTaskControl object is never destroyed.

I am limited to using Unobserved since I have an arbitrary number of tasks and I do not want to store them in a dedicated variable or a list - removing a task from that list would be a hustle in itself since it can only be done from the task body.

Am I missing something? I thought that IOmniTaskControl.Unobserved will destroy itself after it runs.

Upvotes: 1

Views: 46

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

I think I've never answered my own question that quickly :-)

TOmniTaskControl is destructed from the TOmniEventMonitor.WndProc window proc, which of course only runs if there is a message loop in the thread where the monitor was created by the task. In my case, the tasks were created from a dedicated thread (I used TThread.CreateAnonymousThread). Adding a message loop to my thread proc (which runs an infinite while loop) fixed the problem:

  //Omni thread library uses Windows messages to communicate with itself
  while PeekMessage(wMsg, 0, 0, 0, PM_REMOVE or PM_NOYIELD) do begin
    TranslateMessage(wMsg);
    DispatchMessage(wMsg)
  end;

Upvotes: 1

Related Questions