您的位置:首页 > 其它

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

2011-11-24 00:36 288 查看
直接贴代码,简单不用说明:

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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: