zoukankan      html  css  js  c++  java
  • 让应用程序单例方式运行

                                         让应用程序单例方式运行

                                            周银辉

    小经验,做个笔记:

        public partial class App : Application
        {
            
    public App()
            {
                
    //不一定要是在Startup时,请选择适合自己应用程序的合理时间
                Startup += AppStartup;
            }

            
    void AppStartup(object sender, StartupEventArgs e)
            {
                
    bool createNew;
                var mutex 
    = new Mutex(true"TheIdOfThisMutex"out createNew);

                
    if (!createNew)
                {
                    MessageBox.Show(
    "another instance is running");
                    
    //记得在关闭程序前关闭mutex,否则进程有可能不退出
                    mutex.Close();
                    
    //不一定要调用Shutdown方法,请选择适合自己应用程序的退出方式
                    Shutdown();
                }
                
    else
                {
                    
    //记得正常退出程序是关闭mutex
                    Exit += delegate{ mutex.Close(); };
                }
            }
        }
  • 相关阅读:
    最全的常用正则表达式大全
    服务器调用JS
    ASP-----分页功能的实现
    流操作text文件------读取、保存文档
    linq to sql 增删改查
    数据库的高级应用
    SQL---------表的约束
    面向对象--继承练习
    Winform---文件夹操作
    面向对象--里氏转换练习
  • 原文地址:https://www.cnblogs.com/zhouyinhui/p/1587246.html
Copyright © 2011-2022 走看看