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?
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