gpt4 book ai didi

c# - 关闭表单时我得到 An exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll but was not handled in user code

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:46 26 4
gpt4 key购买 nike

附加信息:无法访问已处置的对象。它突出显示了这行代码:

 if (this.InvokeRequired)
{
this.Invoke(new TelemetryData(Telemetry_Data), new object[2] {data, updated});
return;
}

当我尝试退出我的应用程序并抛出此异常时会发生这种情况。不确定你们需要什么代码来帮助我解决这个问题,但这是我的 onClosing 代码:

 private void FleetTrack_FormClosing(object sender, FormClosingEventArgs e)
{
if (lblFTstatus.Text == "CONNECTED")
{
DialogResult dialog = MessageBox.Show("You are currently connected to FleetTrack™\n\nIf you exit now, you will lose all progress on this job.",
"Exit FleetTrack™", MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
dbConnect.Delete();
Application.ExitThread();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
else if (lblFTstatus.Text == "DISCONNECTED")
{
Application.ExitThread();

}
}

最佳答案

您有一个后台线程试图通过 Invoke 编码到 GUI 线程。您关闭了表单,但后台线程继续运行。当 Invoke 被调用时,表单被释放,因此您会得到一个 ObjectDisposedException

肮脏的“修复”是捕获异常。由于您要关闭,因此您可能不在乎。但真正的解决方法是在表单关闭之前关闭后台线程。

关于c# - 关闭表单时我得到 An exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll but was not handled in user code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29290483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com