是否从主线程调用TIdTCPServer.OnConnect?

问题描述

我不知道在TIdTcpserver.OnConnectOndisconnect事件中是否可以从主线程访问数据。是从主线程还是其他线程调用方法?我想在TMemo中记录IP,然后启用或禁用某些按钮。

解决方法

TIdTCPServer是一个多线程组件。其OnConnectOnDisconnectOnExecuteOnException事件均在由事件的TIdContext对象表示的辅助线程的上下文中触发。该线程在TIdContext.Connection属性中管理客户端TCP连接。

OnListenException事件是在另一个工作线程的上下文中触发的,该工作线程侦听客户端连接并创建TIdContext线程。

是的,TIdTCPServer事件处理程序在访问UI控件和其他共享数据时必须与主线程同步。

,

我相信,它是在其自己的线程上下文中调用的。更好地使用Synchronize()或Queue()进行保护...示例:

    def Database_connection():
        try:
            # PostgreSQL connection
            Postgres_connection = psycopg2.connect(user="postgres",password="password",host="127.0.0.1",port="5432",database="Maps")
            Postgres_cursor = Postgres_connection.cursor()
        except (Exception,psycopg2.Error) as error1:
            print("Error while connecting to PostgreSQL",error1)
            return Postgres_connection,Postgres_cursor
    
    def Database_connection_close():
        Postgres_connection=Database_connection()
        Postgres_connection.close()