没错,开始很简单,懂NET编程的人,懂IDE的人都行,你行我行大家行。
来别废话,对抄下面这段代码就对了,重复打开有没有,Log4Net应用了用没有,其他异常抛出有没有,如果没有,恭喜你,你接下来,还是会回来抄这段话。
来抄过去,代码报错不报错不清楚,反正我手动剔除不必要的设置,但抄了心里就要有底,为什么要加这些代码。
来来来,本着不解释的精神,顺便说两句。
如果对部署没有经验的人,一定要警惕程序是否重复打开,因为当你编译好程序给别人后,你永远不知道它回去拿去干什么,如果涉及到硬件初始或者其他问题,重复打开问题,总是会多给你一点问题,总之可能是问题,所以还是谨慎一点,加吧。
Log4Net是每个NET开发员逃不了的东西,你就算上天了,你还是得先给他配进去,第一次配置后有经验了就知道了。
还有就是UI线程报错,NET没有UI界面,你还用什么NET,用其他语言搞去,UI会不会报错我不清楚,UI异常捕获会不会到这里我也不清楚,总之放着它,所以先抄再说。
using System;
using System.Configuration;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
int processCount = 0;
Process[] pa = Process.GetProcesses();//获取当前进程数组。
foreach (Process PTest in pa)
{
if (PTest.ProcessName == Process.GetCurrentProcess().ProcessName)
{
if(!string.IsNullOrEmpty(PTest.MainWindowTitle))
processCount += 1;
}
}
if (processCount > 0)
{
//如果程序已经运行,则给出提示。并退出本进程。
DialogResult dr;
dr = MessageBox.Show(Process.GetCurrentProcess().ProcessName + "程序已经在运行!", "退出程序", MessageBoxButtons.OK, MessageBoxIcon.Error);
//可能你不需要弹出窗口,在这里可以屏蔽掉
return; //Exit;
}
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("app.config"));
//设置应用程序处理异常方式:ThreadException处理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
//MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
//MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
/// <summary>
/// 生成自定义异常消息
/// </summary>
/// <param name="ex">异常对象</param>
/// <param name="backStr">备用异常消息:当ex为null时有效</param>
/// <returns>异常字符串文本</returns>
static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
if (ex != null)
{
if (ex.InnerException != null)
{
sb.AppendLine("【异常类型】:" + ex.InnerException.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.InnerException.Message);
sb.AppendLine("【堆栈调用】:" + ex.InnerException.StackTrace);
}
else
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
}
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}