C# 试图关闭窗体时发生ObjectDisposedException

C# 试图关闭窗体时发生ObjectDisposedException,c#,C#,当试图关闭表单时,我在线程上得到一个System.ObjectDisposedException。我尝试了这个类似问题的建议,但无法阻止例外情况的提出 MainForm.cs static ConcurrentDictionary<string, JoystickProcess> clientToDevice; private Thread listenThread; public MainForm() { InitializeComponent(); Netwo

当试图关闭表单时,我在线程上得到一个
System.ObjectDisposedException
。我尝试了这个类似问题的建议,但无法阻止例外情况的提出

MainForm.cs

static ConcurrentDictionary<string, JoystickProcess> clientToDevice;
private Thread listenThread;

public MainForm()
{
    InitializeComponent();

    Network network = new Network(this);
    clientToDevice = new ConcurrentDictionary<string, JoystickProcess>();

    listenThread = new Thread(network.receive);

    // Start threads in background
    listenThread.IsBackground = true;
    listenThread.Start();
}

public JoystickProcess getClient(string client)
{
    if (clientList.InvokeRequired)
    {
        // Causes System.ObjectDisposedException
        return (JoystickProcess) Invoke(new Func<JoystickProcess>(() => getClient(client)));
    }
    else
    {
        JoystickProcess process;
        if (clientToDevice.TryGetValue(client, out process))
            return process;
        return null;
    }
}
private MainForm main;
private List<string> ipList = new List<string>();

public Network(MainForm main)
{
    this.main = main;
}

public void receive() 
{
    int port = 1608;
    UdpClient client = new UdpClient(port);
    IPEndPoint end = new IPEndPoint(IPAddress.Any, port);

    String addr;
    byte[] recv;
    JoystickProcess process;

    while (true)
    {
        // Wait for data to be received
        recv = client.Receive(ref end);

        addr = end.Address.ToString();

        if (ipList.Contains(addr))
        {
            // Causes System.ObjectDisposedException
            process = main.getClient(addr);

            if (process != null && process.getDevice() != -1)
            {
                Task.Run(() => process.updateDevice(recv));
            }
        }
        else
        {
            ipList.Add(addr);
            main.addClient(addr);
        }
    }
}   
静态ConcurrentDictionary客户端到设备;
私有线程listenThread;
公共表格(
{
初始化组件();
网络=新网络(本);
clientToDevice=新的ConcurrentDictionary();
listenThread=新线程(network.receive);
//在后台启动线程
listenThread.IsBackground=true;
listenThread.Start();
}
公共JoystickProcess getClient(字符串客户端)
{
if(clientList.invokererequired)
{
//导致System.ObjectDisposedException异常
return(JoystickProcess)Invoke(newfunc(()=>getClient(client));
}
其他的
{
JoystickProcess过程;
if(clientToDevice.TryGetValue(客户端,输出进程))
返回过程;
返回null;
}
}
Network.cs

static ConcurrentDictionary<string, JoystickProcess> clientToDevice;
private Thread listenThread;

public MainForm()
{
    InitializeComponent();

    Network network = new Network(this);
    clientToDevice = new ConcurrentDictionary<string, JoystickProcess>();

    listenThread = new Thread(network.receive);

    // Start threads in background
    listenThread.IsBackground = true;
    listenThread.Start();
}

public JoystickProcess getClient(string client)
{
    if (clientList.InvokeRequired)
    {
        // Causes System.ObjectDisposedException
        return (JoystickProcess) Invoke(new Func<JoystickProcess>(() => getClient(client)));
    }
    else
    {
        JoystickProcess process;
        if (clientToDevice.TryGetValue(client, out process))
            return process;
        return null;
    }
}
private MainForm main;
private List<string> ipList = new List<string>();

public Network(MainForm main)
{
    this.main = main;
}

public void receive() 
{
    int port = 1608;
    UdpClient client = new UdpClient(port);
    IPEndPoint end = new IPEndPoint(IPAddress.Any, port);

    String addr;
    byte[] recv;
    JoystickProcess process;

    while (true)
    {
        // Wait for data to be received
        recv = client.Receive(ref end);

        addr = end.Address.ToString();

        if (ipList.Contains(addr))
        {
            // Causes System.ObjectDisposedException
            process = main.getClient(addr);

            if (process != null && process.getDevice() != -1)
            {
                Task.Run(() => process.updateDevice(recv));
            }
        }
        else
        {
            ipList.Add(addr);
            main.addClient(addr);
        }
    }
}   
private main窗体main;
私有列表ipList=新列表();
公共网络(主窗体主窗体)
{
this.main=main;
}
公共接收()
{
int端口=1608;
UdpClient客户端=新的UdpClient(端口);
IPEndPoint end=新IPEndPoint(IPAddress.Any,端口);
字符串地址;
字节[]recv;
JoystickProcess过程;
while(true)
{
//等待接收数据
recv=客户端接收(参考端);
addr=end.Address.ToString();
if(ipList.Contains(地址))
{
//导致System.ObjectDisposedException异常
进程=main.getClient(addr);
if(process!=null&&process.getDevice()!=-1)
{
Task.Run(()=>process.updateDevice(recv));
}
}
其他的
{
ipList.Add(地址);
main.addClient(addr);
}
}
}   

我最后使用布尔值检查表单是否关闭。如果是,则返回
null
,然后在线程本身中检查该值。我很抱歉没有把我的帖子写得那么详细

public JoystickProcess getClient(string client)
{
    if (clientList.InvokeRequired)
    {
        if(shutdown)
            return null;
        return (JoystickProcess) Invoke(new Func<JoystickProcess>(() => getClient(client)));
    }
    else
    {
        JoystickProcess process;
        if (clientToDevice.TryGetValue(client, out process))
            return process;
        return null;
    }
}
public-JoystickProcess-getClient(字符串客户端)
{
if(clientList.invokererequired)
{
如果(关闭)
返回null;
return(JoystickProcess)Invoke(newfunc(()=>getClient(client));
}
其他的
{
JoystickProcess过程;
if(clientToDevice.TryGetValue(客户端,输出进程))
返回过程;
返回null;
}
}

如果说您遇到异常,然后在没有提供堆栈跟踪或异常实际发生位置指示的情况下转储一堆代码,则很难诊断问题。问题发生的位置实际上在代码中指示。@Raggeth,我建议调试代码并检查所有变量和期望值。。您可能正在尝试处理一个已经为空的对象。您能澄清一下“main”在哪里声明和实例化吗。它不会出现在您的示例代码的其他任何地方,它是最有可能导致该错误的原因。@Ulric抱歉,请参阅更新的帖子。