catch (Exception e)
{
new CCatchEx(e,"数据库连接失败,将退出程序。错误信息:");
}执行了上面的代码之后,我的本意是要退出程序的,但程序没有退出而是继续执行后面的代码。
CCatchEx类我是这么写的:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;namespace 质量管理
{
class CCatchEx
{
public CCatchEx(Exception e)
{
forAll();
MessageBox.Show(e.Message, "错误提示");
} public CCatchEx(Exception e, string sErrString, bool bToExit)
{
forAll();
MessageBox.Show(sErrString + '\n' + e.Message, "错误提示");
if (bToExit) Application.Exit(0);
} private void forAll()
{
}
}
}
{
new CCatchEx(e,"数据库连接失败,将退出程序。错误信息:");
}执行了上面的代码之后,我的本意是要退出程序的,但程序没有退出而是继续执行后面的代码。
CCatchEx类我是这么写的:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;namespace 质量管理
{
class CCatchEx
{
public CCatchEx(Exception e)
{
forAll();
MessageBox.Show(e.Message, "错误提示");
} public CCatchEx(Exception e, string sErrString, bool bToExit)
{
forAll();
MessageBox.Show(sErrString + '\n' + e.Message, "错误提示");
if (bToExit) Application.Exit(0);
} private void forAll()
{
}
}
}
new CCatchEx(e,"数据库连接失败,将退出程序。错误信息:",true);
的时候,不退出程序,请同志们帮忙
{
Application.Exit();
MessageBox.Show("sdfsdf");
}好像这样是退出不了阿,不过下面这样就可以退出了,是不是Application.Exit();后面不能有代码阿?
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("sdfsdf");
Application.Exit();
}
Application.Exit(0);不能通过编译
关于在构造函数,我试了在构造函数外退出,也是无法退出~请大家费心了~ 另外帖出发现~ 我的Main函数是这么写的: static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
a: Form1 form1 = new Form1();
Application.Run(form1);
}而错误是出在form1.Form1_Load中调用的一个函数中,程序要在执行完Form_Load,还要执行完 protected override void Dispose(bool disposing) 才退出。请问各位,C#里到底有没有一个函数,可以立即退出程序呢?
就已经是立即推出了啊你的form_load怎么写的
{
Application.Exit();
MessageBox.Show("sdfsdf");
}
1. 给你的CCatchEx添加一个事件比如OnExit,在需要告诉主程序退出的时候触发这个事件
2. 主程序给CCatchEx注册事件,CCatchEx_OnExit(),在里面执行Application.Exit();
System.Environment.Exit(0);万试万灵,无论在主线程和其它线程,只要执行了这句,都可以把程序结束干净。