C# 表单不会在 FormClosing 事件上关闭

发布于 2024-09-25 22:34:45 字数 162 浏览 0 评论 0原文

将以下代码添加到我的代码隐藏后,我的表单不会关闭。

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    MyThreadingObj.Dispose();
}

After I added the following code to my code-behind, my form doesn't get closed.

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    MyThreadingObj.Dispose();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2024-10-02 22:34:45

听起来添加上述代码会阻止您的 Form 关闭。如果是这样,那么最可能的原因是 MyTHreadingObj.Dispose() 语句引发异常。尝试将语句包装在 try/catch 中,看看是否是这种情况

try {
  MyThreadingObj.Dispose();
} catch ( Exception e ) { 
  Console.WriteLine(e);
}

It sounds like adding the above code prevents your Form from closing. If so then the most likely cause is that the MyTHreadingObj.Dispose() statement is throwing an exception. Try wrapping the statement in a try/catch and seeing if this is the case

try {
  MyThreadingObj.Dispose();
} catch ( Exception e ) { 
  Console.WriteLine(e);
}
骷髅 2024-10-02 22:34:45
protected override void OnFormClosing(FormClosingEventArgs e)
        {            
            base.OnFormClosing(e);
            if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
            {
                Dispose(true);
                Application.Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private DialogResult PreClosingConfirmation()
        {
            DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return res;
        }
protected override void OnFormClosing(FormClosingEventArgs e)
        {            
            base.OnFormClosing(e);
            if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
            {
                Dispose(true);
                Application.Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private DialogResult PreClosingConfirmation()
        {
            DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return res;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文