Board index » delphi » TComServer. Found a bug?

TComServer. Found a bug?

Delphi 4 / DCOM

Here's the problem.  I don't want the automation server to close the
main thread when the
last client disconnects.  If the automation server is started manually,
then it can only be shut down manually.  But, if the server is started
via automation, then you have to setup an event handler for the
ComServer object's OnLastRelease event, and set the "ShutDown" var
parameter to False in the handler.

O.K.  So far so good.  The server stays in memory when the last client
disconnects.  But, now when a new client tries to connect to the server,
an exception is raised

"Invoked object has disconnected from its clients".  You guessed it, no
connection is made.

Has anyone else ran into this problem? If so, please, share the wealth!

Thanks
Todd E.

 

Re:TComServer. Found a bug?


Quote
Todd Erickson wrote...
>Delphi 4 / DCOM

[Portions extracted]

Quote
>O.K.  So far so good.  The server stays in memory when the last client
>disconnects.  But, now when a new client tries to connect to the server,
>an exception is raised

>"Invoked object has disconnected from its clients".  You guessed it, no
>connection is made.

[Remainder excised]

O.K.  This is a bit of a Catch-22.  You mentioned "DCOM".  If DCOM is
present and enabled then your system(s) support(s) certain COM API functions
that are meant to provide better threading support and lifetime management
in the out-of-process scenario.  These functions are CoAddRefServerProcess
and CoReleaseServerProcess.  Delphi 4 uses them when they are present.  This
will occur on systems equipped  with DCOM for Win 95 or with Windows NT 4
w/service pack 2 or later.

Each call to ComServer.CountObject(False) will cause CoReleaseServerProcess
to be called.  On the final call to this function, the COM runtime will also
execute CoSuspendClassObjects.  It does this to ensure that the server does
not attempt to serve up objects while the it is in the process of shutting
down.

So, although you've set the Shutdown parameter to false, COM-wise, your
server is unavailable.

My suggestions: Call CoResumeClassObjects in your OnLastRelease event
handler.

Or...

Place the following line somewhere early in the server initialization.

(ComClassManager.GetFactoryFromClassID(MyClassID) as
IClassFactory).LockServer(True);

BTW, I've yet to try any of these, but they seem like your logical choices.

G'Luck
Roland Bouchereau

Re:TComServer. Found a bug?


Found the problem and the fix:

When the server is started via automation, when a new client connects after
the OnLastRelease event, a new instance of the server is started!  This
happens every time the client count reaches zero and a new client connnects!

Here's the fix:  You'll have to modify the ComServ unit.  ** - added line

** procedure InitComServer; forward;

procedure TComServer.LastReleased;
var
  Shutdown: Boolean;
begin
  if not FIsInprocServer then
  begin
    Shutdown := FStartMode = smAutomation;
    try
      if Assigned(FOnLastRelease) then FOnLastRelease(Shutdown);
    finally
      if Shutdown then PostThreadMessage(MainThreadID, WM_QUIT, 0, 0)
**      else if StartMode = smAutomation then
**       InitComServer;
    end;
  end;
end;

Quote
Todd Erickson wrote:
> Delphi 4 / DCOM

> Here's the problem.  I don't want the automation server to close the
> main thread when the
> last client disconnects.  If the automation server is started manually,
> then it can only be shut down manually.  But, if the server is started
> via automation, then you have to setup an event handler for the
> ComServer object's OnLastRelease event, and set the "ShutDown" var
> parameter to False in the handler.

> O.K.  So far so good.  The server stays in memory when the last client
> disconnects.  But, now when a new client tries to connect to the server,
> an exception is raised

> "Invoked object has disconnected from its clients".  You guessed it, no
> connection is made.

> Has anyone else ran into this problem? If so, please, share the wealth!

> Thanks
> Todd E.

Other Threads