C# 调整控件大小时TCPClient关闭

C# 调整控件大小时TCPClient关闭,c#,windows,winforms,user-controls,tcpclient,C#,Windows,Winforms,User Controls,Tcpclient,在一个C#Winforms项目中,我有一个UserControl类,它有一个TcpClient作为私有成员。 当用户拖动UserControl的父类或以其他方式调整其大小时,TcpClient被设置为null。当UserControl的父控件开始最大化或最小化时,TcpClient未设置为null。处理TcpClient的代码在单独的thead中运行。我找不到其他有这个问题的用户 我尝试过在UserControl的构造函数和析构函数上设置断点,但没有效果。代码如下所示: public parti

在一个C#Winforms项目中,我有一个UserControl类,它有一个TcpClient作为私有成员。 当用户拖动UserControl的父类或以其他方式调整其大小时,TcpClient被设置为null。当UserControl的父控件开始最大化或最小化时,TcpClient未设置为null。处理TcpClient的代码在单独的thead中运行。我找不到其他有这个问题的用户

我尝试过在UserControl的构造函数和析构函数上设置断点,但没有效果。代码如下所示:

public partial class IPCInterface: UserControl
{
    private TcpClient aClient = null;
    private bool IsRunning = true;
    private Thread ShellThread = null;
    // [SNIP]
    private void MainLoop()
    {
        // The exception occurs in this function when resizing
        try
        { 
            // The exception is thrown here, aClient.Client.Get returns null -> nullreference exception
            while (IsRunning && aClient.Client.Connected == true)
            {
                while (aClient.Client.Available > 0)
                    ShellInputOutputBox.Invoke(
                        (MethodInvoker)delegate () 
                        {
                            ShellInputOutputBox.Text += ReceiveString();
                        });

                Thread.Sleep(100);
            }

            if(aClient.Client.Connected == true)
                aClient.Client.Close();
        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  // The exception occurs here when resizing
        }

    }

    public IPCInterface(TcpClient IPCStream)
    {
        InitializeComponent();

        aClient = IPCStream;
        ShellThread = new Thread(() => MainLoop());
        ShellThread.Start();
    }
        public static TcpClient OpenSession(string ServerIp, int ServerPort)
        {
            cServerControl Result = new cServerControl(ServerIp, ServerPort);
            Result.Connect();

            // [SNIP]

            return Result.TcpClient;
        }
我不知道为什么会发生这种事。有人知道我怎样才能阻止插座的关闭吗

其他信息:


UserControl本身位于选项卡界面内,选项卡界面也位于选项卡界面内(我不是UX设计师…)。所有这些界面都固定在其父界面的外边缘,以便在用户调整主窗体的大小时移动它们。

我已经解决了这个问题。事实证明,这是一个对象所有权问题,再加上winforms在调整窗体大小时显然会触发垃圾回收

在代码的其他地方,我创建了UserControl使用的连接,如下所示:

public partial class IPCInterface: UserControl
{
    private TcpClient aClient = null;
    private bool IsRunning = true;
    private Thread ShellThread = null;
    // [SNIP]
    private void MainLoop()
    {
        // The exception occurs in this function when resizing
        try
        { 
            // The exception is thrown here, aClient.Client.Get returns null -> nullreference exception
            while (IsRunning && aClient.Client.Connected == true)
            {
                while (aClient.Client.Available > 0)
                    ShellInputOutputBox.Invoke(
                        (MethodInvoker)delegate () 
                        {
                            ShellInputOutputBox.Text += ReceiveString();
                        });

                Thread.Sleep(100);
            }

            if(aClient.Client.Connected == true)
                aClient.Client.Close();
        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  // The exception occurs here when resizing
        }

    }

    public IPCInterface(TcpClient IPCStream)
    {
        InitializeComponent();

        aClient = IPCStream;
        ShellThread = new Thread(() => MainLoop());
        ShellThread.Start();
    }
        public static TcpClient OpenSession(string ServerIp, int ServerPort)
        {
            cServerControl Result = new cServerControl(ServerIp, ServerPort);
            Result.Connect();

            // [SNIP]

            return Result.TcpClient;
        }

我认为C#得出的结论是,由于cServerControl是在一个独立于稍后将与TcpClient交互的线程中创建的,因此它可以安全地处理TcpClient。忽略TcpClient显式传输到新线程的事实。更改代码以返回cServerControl(并在UserControl中使用cServerControl而不是TcpClient)修复了该问题,由于cServerControl现在未标记为可处置。

在代码中,我看到一条注释
//如果您已找到异常,则在调整大小时,此函数中会发生异常。
最好将异常信息发布。完成后,异常会在while循环的coditional语句中抛出。aClient.Client变量设置为null。我认为它与调整大小无关。什么东西是空的?调试时会发生什么?听起来像是你的应用程序出现了某种一般的结构问题。可能有很多东西从代码中的其他地方与UI交互…@AkshayMahajan当应用程序中断时,其他线程都没有做任何有趣的事情。没有调用任何其他涉及TcpClient的函数。不调用析构函数,也不调用构造函数。我确实看到,在抛出异常之前,我的应用程序的布局发生了变化,这使我相信这与调整应用程序的大小有关。