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

                }
            }
        }
    }

  • 相关阅读:
    Delphi Class of 类引用
    Class-reference types 类引用类型--快要失传的技术
    最简单的TabHost
    修改一些IntelliJ IDEA 11的设置,使Eclipse的使用者更容易上手(转)
    uva 10494
    uva748
    uva 465
    高精度
    uva 694
    uva414
  • 原文地址:https://www.cnblogs.com/tuzhiye/p/1381686.html
Copyright © 2011-2022 走看看