static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);//其他线程
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); //ui线程
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmEDCE());
}
else
{
MessageBox.Show("已经运行了此程式的一个实例!");
}
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
ErrorLog log = new ErrorLog();
log.WriteThreadErrorLog(e.ExceptionObject.ToString());
MessageBox.Show(e.ExceptionObject.ToString(), "提示");
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
ErrorLog log = new ErrorLog();
log.WriteThreadErrorLog(e.Exception.StackTrace);
MessageBox.Show(e.Exception.Message, "提示");
}
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;
}
}