static void Main()
{
try
{
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += Application_ThreadException;
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
//0106add 一次打开一个应用程序
Process instance = RunningInstance();
if (instance != null)
{
if (instance.MainWindowHandle.ToInt32() == 0) //是否托盘化
{
MessageBox.Show("程序已打开并托盘化");
return;
}
//1.2 已经有一个实例在运行
HandleRunningInstance(instance);
return;
}
ConfigTool.Path = ParamCache.formPath + "\Config\config.xml";
ConfigTool.GetConfigDic();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
BonusSkins.Register();
LoginForm login = new LoginForm();
login.ShowDialog();
if (login.DialogResult == DialogResult.OK)
{
login.Dispose();
Application.Run(new MainForm());
}
}
catch (Exception ex)
{
var strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now + "
";
var str = string.Format(strDateInfo + "异常类型:{0}
异常消息:{1}
异常信息:{2}
",
ex.GetType().Name, ex.Message, ex.StackTrace);
LogTool.Info(str);
MessageBox.Show("发生错误,请查看程序日志!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
}
#region 确保程序只运行一个实例
private static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//如果实例已经存在则忽略当前进程
if (process.Id != current.Id)
{
//保证要打开的进程同已经存在的进程来自同一文件路径
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\") == current.MainModule.FileName)
{
//返回已经存在的进程
return process;
}
}
}
return null;
}
//3.已经有了就把它激活,并将其窗口放置最前端
private static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
#endregion
#region 错误处理
/// <summary>
///错误弹窗
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str;
var strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now + "
";
var error = e.Exception;
if (error != null)
{
str = string.Format(strDateInfo + "异常类型:{0}
异常消息:{1}
异常信息:{2}
",
error.GetType().Name, error.Message, error.StackTrace);
}
else
{
str = string.Format("应用程序线程错误:{0}", e);
}
LogTool.Info(str);
MessageBox.Show("发生错误,请查看程序日志!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var error = e.ExceptionObject as Exception;
var strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now + "
";
var str = error != null ? string.Format(strDateInfo + "Application UnhandledException:{0};
堆栈信息:{1}", error.Message, error.StackTrace) : string.Format("Application UnhandledError:{0}", e);
LogTool.Info(str);
MessageBox.Show("发生错误,请查看程序日志!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
#endregion
/// <summary>
/// 修改程序在注册表中的键值
/// </summary>
/// <param name="flag">1:开机启动</param>
private void StartUp(string flag)
{
string path = Application.StartupPath;
string keyName = path.Substring(path.LastIndexOf("\") + 1);
Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (flag.Equals("1"))
{
if (Rkey == null)
{
Rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
}
Rkey.SetValue(keyName, path + @"KBDMtrlSystemForm.exe");
}
else
{
if (Rkey != null)
{
Rkey.DeleteValue(keyName, false);
}
}
}