Delphi - Omnithreadlibrary,控制台应用程序中的主线程死亡
Delphi - Omnithreadlibrary, death of main thread in console application
我在控制台应用程序的 BackgroundWorker(高级 OmniThreadLibrary 组件)中的主线程有问题。主线程(整个应用程序)中的对象一旦为后台任务安排 WorkItems 就会死亡。主线程不等待 OnRequestDone 方法调用。
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;
有什么办法可以解决这种情况吗?
OTL 依赖于其主线程中的 Windows 消息队列。您必须发送消息。这在 GUI 应用程序中自然会发生,但不会在控制台应用程序中发生。向您的程序添加一个消息循环。
示例编号 62 证明了这一点:https://github.com/gabr42/OmniThreadLibrary/tree/master/tests/62_Console
我在控制台应用程序的 BackgroundWorker(高级 OmniThreadLibrary 组件)中的主线程有问题。主线程(整个应用程序)中的对象一旦为后台任务安排 WorkItems 就会死亡。主线程不等待 OnRequestDone 方法调用。
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;
有什么办法可以解决这种情况吗?
OTL 依赖于其主线程中的 Windows 消息队列。您必须发送消息。这在 GUI 应用程序中自然会发生,但不会在控制台应用程序中发生。向您的程序添加一个消息循环。
示例编号 62 证明了这一点:https://github.com/gabr42/OmniThreadLibrary/tree/master/tests/62_Console