zoukankan      html  css  js  c++  java
  • 保证程序只有一个实例运行并且先将旧实例关闭再运行新实例

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;
    using System.Diagnostics;
    using System.Reflection;

    namespace MyClient
    {
        
        
    static class Program
        {

            
    /// <summary>
            
    /// The main entry point for the application.
            
    /// </summary>
            [STAThread]
            
    static void Main(string[] args)
            {

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
    false);
                
    //bool blnCreate;//Another method to make sure only one instance
                
    //Mutex m = new Mutex(true, "MyClient", out blnCreate);
                
    //if (blnCreate)
                CloseRunningInstance();
                Application.Run(
    new MainFrm());
            }
            
    //Close the existed instance of my application
            static void CloseRunningInstance()
            {
                Process current 
    = Process.GetCurrentProcess();
                Process[] processes 
    = Process.GetProcessesByName(current.ProcessName);
                
    //Loop through the running processes in with the same name    
                foreach (Process process in processes)
                {
                    
    //Ignore the current process    
                    if (process.Id != current.Id)
                    {
                        
    //Make sure that the process is running from the exe file.    
                        if (Assembly.GetExecutingAssembly().Location.Replace("/""\\"== current.MainModule.FileName)
                        {
                            
    //Close the other process instance.   
                            process.Kill();
                            process.WaitForExit();
                        }
                    }
                }
            } 
        }
    }
  • 相关阅读:
    干货分享|安全测试起航之旅
    干货|宏巍软件之Java线程监控之旅
    大数据时代,业务运维驱动下的企业变革
    什么是业务运维,企业如何实现互联网+业务与IT的融合
    不谈业务运维的IT主管早晚被淘汰 这里是10条干货
    详解APM数据采样与端到端
    CTO对话:云端融合下的移动技术创新
    【1111元天天拿!充一送二玩真哒!】双十一任性活动
    引领手机流量营销 嘿嘿流量打造多场景专业服务
    “烧钱补贴”下的O2O该何去何从?
  • 原文地址:https://www.cnblogs.com/coderzh/p/989375.html
Copyright © 2011-2022 走看看