Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Omnithreadlibrary, death of main thread in console application

I have a problem with main thread in BackgroundWorker (high level OmniThreadLibrary component) in console application. Object in main thread (whole application) dies as soon as it schedules WorkItems for background tasks. Main thread doesnt wait for OnRequestDone method calling.

procedure TEntityIndexer.StartReindex;
begin
  if LoadTable then
    // in ProcessRecords method I schedule WorkItems for BackgroundWorker
      ProcessRecords;
  // when ProcessRecords method is done, application is at the end and
  // main thread is destoryed, so object in main thread is destroyed
  // and BackgroundWorker in object in main thread is destroyed too
end;

procedure TEntityIndexer.ProcessRecords;
var
  _id: Integer;
  _omniValue: TOmniValue;
begin
  FVTable.First;
  while not FVTable.Eof do
  begin
    _id := FVTable.FieldByName('record_id').AsInteger;
    WriteLogText(cProcesIndexLog, 'ID=' + IntToStr(_id) + '....PROCESS STARTED');

    _omniValue := TOmniValue.CreateNamed(
      [ovIdKey, _id,
      ovXMLKey, FVTable.FieldByName('mx').AsString,
      ovGenKey, FVTable.FieldByName('created_str').AsString
      ]);
    FBackgroundWorker.Schedule(FBackgroundWorker.CreateWorkItem(_omniValue));

    FVTable.Next;
  end;
end;

Is any solution to solve this situation?

like image 851
Michal Miškov Avatar asked Jan 17 '26 23:01

Michal Miškov


1 Answers

OTL relies on the Windows message queue in its main thread. You must pump messages. This happens naturally in a GUI app, but not a console app. Add a message loop to your program.

Example number 62 demonstrates this: https://github.com/gabr42/OmniThreadLibrary/tree/master/tests/62_Console

like image 106
David Heffernan Avatar answered Jan 19 '26 17:01

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!