C# does not release the subform object after closing the subform

C# closes the subform but does not release the subform object

When working on the project, I made a separate subform for TCP communication connection, but after closing the subform, the connection was interrupted, so how to close the subform without releasing the subform object?

Find the Dispose method in the subform Designer.cs , as shown in the figure:

 Modify this method to:

        protected override void Dispose(bool disposing)
        {
            Hide();
        }

Add a close function in the subform:

        public void Close()
        {
            this.Dispose();
        }

When the main form is closed, the subform object must be destroyed. So add the following code to the FormClosed event of the main form:

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            tcpForm.Close();//tcpForm为子窗体
        }

 

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/109125262