Connection Indy、TidNotify和关闭TidCpServer
我有一个TidTCPServer,它在onExcecute事件中使用数据库操作(通过使用tidtnotify)。一切都很好,而不是关闭应用程序。 在关闭应用程序期间,我不知道所有Notify实例是否都完成了它们的工作,通常我会得到运行时错误216(我认为我在“Notify”工作结束之前关闭了数据库)。 是否有任何方法可以检查-是否有等待旧的通知帖子或不确定我是否可以关闭应用程序。 另一个问题是如何保护TidTCPServer在关闭服务器进程期间不接受新连接。 我使用下面这样的代码,但仍然得到了错误Connection Indy、TidNotify和关闭TidCpServer,connection,indy,Connection,Indy,我有一个TidTCPServer,它在onExcecute事件中使用数据库操作(通过使用tidtnotify)。一切都很好,而不是关闭应用程序。 在关闭应用程序期间,我不知道所有Notify实例是否都完成了它们的工作,通常我会得到运行时错误216(我认为我在“Notify”工作结束之前关闭了数据库)。 是否有任何方法可以检查-是否有等待旧的通知帖子或不确定我是否可以关闭应用程序。 另一个问题是如何保护TidTCPServer在关闭服务器进程期间不接受新连接。 我使用下面这样的代码,但仍然得到了错
type
TShutdownThread = class(TThread)
protected
procedure Execute; override;
end;
procedure TShutdownThread.Execute;
begin
IdTCPServer.Active := false;
end;
//closing...
if IdTCPServer.Active then
begin
with TShutdownThread.Create(false) do
try
WaitFor; // internally processes sync requests...
finally
Free;
end;
end;
有什么方法可以检查-有吗 等待旧的通知帖子或不确定是否可以关闭 应用程序。
TIdNotify
是异步的,它将请求发布到主线程消息队列以供以后执行。TShutdownThread.WaitFor()退出后,挂起的请求可能仍在队列中。您可以调用RTL的CheckSynchronize()
函数来处理任何剩余的请求,例如:
if IdTCPServer.Active then
begin
with TShutdownThread.Create(false) do
try
WaitFor;
finally
Free;
end;
CheckSynchronize;
end;
如何在关闭服务器过程中保护TidTCPServer不接受新连接。
停用TIdTCPServer
时,它会为您关闭其侦听端口。但是,在服务器关闭端口之前接受新客户端的机会很小。服务器将关闭这些连接作为其关闭的一部分,但如果您不想为这些连接调用OnExecute
事件,则可以在停用服务器之前在代码中的某个位置设置一个标志,然后在OnConnect
事件中检查该标志,如果已设置,则立即断开客户端,例如:
var
ShuttingDown: boolean = False;
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
if ShuttingDown then
begin
AContext.Connection.Disconnect;
Exit;
end;
...
end;
...
if IdTCPServer.Active then
begin
ShuttingDown := True;
try
with TShutdownThread.Create(false) do
try
WaitFor;
finally
Free;
end;
CheckSynchronize;
finally
ShuttingDown := False;
end;
end;