• 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(
    "系统只允许运行一个实例!""系统提示");

                }
            }
        }
    }

  • 相关阅读:
    不同包中继承关系访问protected内部类问题
    LinuxMM--MemoryHierarchy
    LinuxMM--Memory Pressure
    多态实验:基类——子类成员
    使用vue开发的word-cloud 词云组件
    基于mui.PopPicker的可多选下拉多选的底部弹出组件
    基于mui.popover的自定义底部弹出框-支持多选和搜索筛选的弹出列表组件改造以及mui.prompt添加自定义内容
    js去空 去除空格
    css+js控制文字显示行数,多出部分显示省略号
    使用vue开发自定义tabs标签页组件
  • 原文地址:https://www.cnblogs.com/tuzhiye/p/1381686.html
Copyright © 2020-2023  润新知