zoukankan      html  css  js  c++  java
  • C#使用Mutex实现系统范围内单实例运行的示例代码

      private static void GlobalMutex()
            {
                // 是否第一次创建mutex
                bool newMutexCreated = false;
                string mutexName = "Global\\" + "tenghoo";
                Mutex mutex = null;
                try
                {
                    mutex = new Mutex(false, mutexName, out newMutexCreated);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    System.Threading.Thread.Sleep(3000);
                    Environment.Exit(1);
                }

                // 第一次创建mutex
                if (newMutexCreated)
                {
                    Console.WriteLine("程序已启动");
                   //todo:此处为要执行的任务
                }
                else
                {
                    Console.Write("另一个窗口已在运行,3秒以后自动关闭。。");
                    System.Threading.Thread.Sleep(1000);
                    Console.Write("1");
                    System.Threading.Thread.Sleep(1000);
                    Console.Write("2");
                    System.Threading.Thread.Sleep(1000);
                    Console.Write("3");
                    Environment.Exit(1);//退出程序
                }
            }

  • 相关阅读:
    spark streaming 程序设置jvm参数
    Continuously INFO JobScheduler:59
    No partition metadata for topic test due to kafka.common.LeaderNotAvailableException
    spark 免密码登录- ssh 指定非22端口
    jvm中的新生代Eden和survivor区
    Swift之控件-UIlabel
    UITextField的placeholder文字的位置,颜色等的自定义设置
    版本更新
    CFBundleVersion与CFBundleShortVersionString,上架注意事项
    上拉刷新,下拉加载
  • 原文地址:https://www.cnblogs.com/sky7034/p/2801278.html
Copyright © 2011-2022 走看看