blackcore

本质的东西,深植于骨骼,扎根于灵魂! 淘实惠,各类电子版书籍

导航

如何只保留一个应用程序实例

直接贴代码,简单不用说明:

    static class Program
{
///<summary>
/// The main entry point for the application.
///</summary>
[STAThread]
static void Main()
{
try
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//订阅ThreadException事件,处理UI线程异常,处理方法为 Application_ThreadException,关于事件的相关知识就不在这叙述了
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//订阅UnhandledException事件,处理非UI线程异常 ,处理方法为 CurrentDomain_UnhandledException
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

//只允许一个应用程序实例
bool createdNew;
Mutex mutex = new Mutex(false, "blackcore.editor.run", out createdNew);
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Default());
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}



posted on 2011-11-24 00:36  blackcore  阅读(249)  评论(0编辑  收藏  举报