Winform deletes the same thread multiple times to create the control program crashes

Recently, when I was doing a configuration file manager, a very strange phenomenon appeared.

I need to use winform to make a program that can manage multiple device configuration files, use UDP communication between devices, the manager sends a request to other devices to get the configuration file, and then sends it back to the lower-level device after modification, I used a singleton data class To manage all configuration files, put them in a List

If only one device is connected, everything works fine

However, when I connect more than two devices, I found that when I click any button multiple times, the UI created reaches a certain total number, and the program crashes, I added GC at the point of cleaning up old controls and creating new ones, and found the same It will get stuck, so I suspect it has something to do with GC, but unfortunately I spent a day and couldn't figure out the problem

Accidentally, I found that after manually adding a piece of configuration data to the form's constructor, it won't get stuck. I don't know why, but I'm only ignorant at the moment, and I hope to understand this problem later.

The solved code is as follows

 public Form1()
        {
    
    
            InitializeComponent();
            //添加模拟数据,如果不添加,多次点击后会报错
            CommonData.Instance.AddConfigData(test3, "192.168.1.36");
        }

        string test3 = "空/1/Default_1/ + ";

Guess you like

Origin blog.csdn.net/weixin_44568736/article/details/122751384