OnTerminate never gets called in COM Server


2005-01-06 10:20:01 PM
delphi49
Hi,
I have a IN-PROCESS COM SERVER that creates a thread to do the actual
work. I want to be able to
gracefully terminate the thread and send the event back to the client.
For that the server exposes a Terminate function for signaling the
thread that it has to terminate. The
client implements the necessary eventsink.
The problem is that the thread never executes the OnTerminate event.
Using a OUT-OF-PROCESS SERVER all this works fine.
Actually, the thread object executes DoTerminate method but it never
arrives to execute
Here's the snipet:
________________________
!!!!!!!!!!!!!!!!!!!! in the classes unit:
function ThreadProc(Thread: TThread): Integer;
var
FreeThread: Boolean;
begin
try
if not Thread.Terminated then
try
Thread.Execute;
except
Thread.FFatalException := AcquireExceptionObject;
end;
finally
FreeThread := Thread.FFreeOnTerminate;
Result := Thread.FReturnValue;
// thread hangs here !!!
Thread.DoTerminate;
// never gets HERE !!!! when a OnTerminate is assigned to the
thread
Thread.FFinished := True;
SignalSyncEvent;
if FreeThread then Thread.Free;
EndThread(Result);
end;
end;
________________________
procedure TThreadConnection.Execute;
begin
FreeOnTerminate := True;
OnTerminate := DoTerminate;
while not Terminated do
begin
try
DoWork;
except
end;
if not Terminated then
Suspend;
end;
end;
________________________
procedure TThreadConnection.DoTerminate(Sender: TObject);
begin
SendDebug(LogMessage+' - TERMINATED !!!! ');
//send the event on the client
(IInterface(FComServer) as IComServerNotify).FireTerminate;
end;
________________________
on the server class:
procedure TComServer.Terminate;
var
retVal: Cardinal;
begin
SendDebug('SERVER: Terminate demand');
FThreadConnection.Terminate;
FThreadConnection.Resume;
end;
________________________
Thanks for help,
Cristian