zoukankan      html  css  js  c++  java
  • C#仅允许一个程序实例运行

    方法1.按如下代码更改
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Reflection;

    namespace WindowsFormsApplicationTest
    {
        
    static class Program
        {
            
    /// <summary>
            
    /// The main entry point for the application.
            
    /// </summary>
            [STAThread]
            
    static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
    false);

                
    //防止启动多个应用和序
                if (RunningInstance() != null)
                {
                    System.Windows.Forms.MessageBox.Show(
    "已经有一个程序在运行");
                    
    return;
                }

                Application.Run(
    new Form1());
            }

            
    public static Process RunningInstance()
            {
                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)
                        {
                            
    //Return the other process instance.
                            return process;
                        }
                    }

                }
                
    //No other instance was found, return null.
                return null;

            } 
        }
    }

    方法2
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;

    namespace TestForm.TreeViewAndMDIForm
    {
        
    static class Program
        {
            
    /// <summary>
            
    /// 应用程序的主入口点。
            
    /// </summary>
            [STAThread]
            
    static void Main()
            {
                
    bool createdNew;
                Mutex m 
    = new Mutex(true"test"out createdNew);
                
    if (createdNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(
    false);
                    Application.Run(
    new Form1());
                    m.ReleaseMutex();
                }
                
    else
                {

                    MessageBox.Show(
    "系统只允许运行一个实例!""系统提示");

                }
            }
        }
    }

  • 相关阅读:
    Windows10打印mumu模拟器日志
    简析快速排序
    数字转换为W,K,结尾,并可指定长度(仅供参考,个人测试使用。)
    简析选择排序
    简析冒泡排序
    NX二次开发 工程图创建孔表功能
    NX二次开发 建模座标和工程图座标映射
    NXOpen绝对座标值转为WCS座标值
    NX二次开发 结合包容盒快速创建WCS
    NX二次开发 数值转NXString字符
  • 原文地址:https://www.cnblogs.com/tuzhiye/p/1381686.html
Copyright © 2011-2022 走看看