避免在 win 表单中从多线程重复调用例程?

发布于 2024-12-10 10:57:45 字数 1731 浏览 1 评论 0原文

我在 Windows 窗体中有一个 Private 对象变量,它执行 tcp/IP 套接字连接并保持连接打开。

在 form_load 上,该对象被初始化,并且表单内有 15-20 个连续运行的线程来访问该对象。在某些情况下,Tcp/Ip 连接可能会丢失。因此,每当我发现连接丢失时,我都会在线程中调用 ReconnectToSocket() 方法。我正在执行以下代码,以确保使用 _ReconnectingSocket 属性仅调用 ReconnectToSocket() 方法一次。但在检查文本日志文件后,我发现这个方法是在每个子线程中调用的。

我如何确保该方法仅被调用一次并避免重复调用。

下面是我的代码。我对任何替代方法都感兴趣,因为我觉得这不是正确的方法。

    bool _bReconnectingSocket = false;//To check if it is currently reconnecting
    readonly object lock_reconnectSocket = new object();
    private bool _ReconnectingSocket
    {
        get
        {
            lock (lock_reconnectSocket)
            {
                return this._bReconnectingSocket;
            }
        }
        set
        {
            lock (lock_reconnectSocket)
            {
                this._bReconnectingSocket = value;
            }
        }
    }


    private void ReconnectToSocket()
    {
        if (!this._ReconnectingSocket)
        {
            this._ReconnectingSocket = true;

            //Each sub thread checks for this variable while looping and exits from the infinite loop
            this._Stop = true;

            //Join all the Sub Threads Before Reconnecting
            foreach (SocketThread thrd in this._subThreadCol)
            {
                try
                {
                    this._objLog.WriteInfo(string.Format("Joining Subthread - {0} for Reconnecting.", thrd.ThrdID));
                    thrd.Join();
                }
                catch { }
            }

            this.ConnectSocket();

            this._ReconnectingSocket = false;
            this._Stop = false;
        }
    }

I have a Private object variable within a Windows form which has performs the tcp/IP socket connection and keeps the connection opened.

On form_load this object is initialized and the form has 15-20 Threads running continously within it which access this object. There are scenarios where in which the Tcp/Ip connection might be lost. SO whenever i find that the connection is lost i call the ReconnectToSocket() Method within the thread. I am performing the below code to ensure that the ReconnectToSocket() method is only called once by using _ReconnectingSocket property. But after checking the Text Log files i found out that this method is called within each sub thread's.

How can i make sure that this method is called only once and avoid repetitive calls.

Below is my code. I am interested in any alternative approach, because i feel that this is not the right approach in doing so.

    bool _bReconnectingSocket = false;//To check if it is currently reconnecting
    readonly object lock_reconnectSocket = new object();
    private bool _ReconnectingSocket
    {
        get
        {
            lock (lock_reconnectSocket)
            {
                return this._bReconnectingSocket;
            }
        }
        set
        {
            lock (lock_reconnectSocket)
            {
                this._bReconnectingSocket = value;
            }
        }
    }


    private void ReconnectToSocket()
    {
        if (!this._ReconnectingSocket)
        {
            this._ReconnectingSocket = true;

            //Each sub thread checks for this variable while looping and exits from the infinite loop
            this._Stop = true;

            //Join all the Sub Threads Before Reconnecting
            foreach (SocketThread thrd in this._subThreadCol)
            {
                try
                {
                    this._objLog.WriteInfo(string.Format("Joining Subthread - {0} for Reconnecting.", thrd.ThrdID));
                    thrd.Join();
                }
                catch { }
            }

            this.ConnectSocket();

            this._ReconnectingSocket = false;
            this._Stop = false;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-12-17 10:57:45

尝试在课堂上写一些类似的东西。您的例程仍然可能被调用多次,但如果 reconnected 字段为 false,则其实际主体一次只会执行一次。

bool reconnected = false;
object lockObject = new object();

void ReconnectToSocket()
{
  lock(lockObject)
  {
    if(!reconnected) { /*do stuff*/; reconnected = true; }
  }
}

Try to write something like that in your class. Your routine still might be called several times, but its actual body will be executed only once at a time, if reconnected field is false.

bool reconnected = false;
object lockObject = new object();

void ReconnectToSocket()
{
  lock(lockObject)
  {
    if(!reconnected) { /*do stuff*/; reconnected = true; }
  }
}
风为裳 2024-12-17 10:57:45

您好,您锁定的对象应该是类的静态私有对象,而不是实例成员。我不确定的一件事是,为什么要在线程之间共享相同的连接,而不是让每个线程打开、使用并立即关闭自己的连接,就像我们对 SqlConnection 所做的那样。

Hi the object you lock against should be static private of the class and not an instance member. One thing I am not sure about is why you are sharing same connection among threads instead of having each thread to open, consume and immediately close its own one like we would do with a SqlConnection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文