用c# 如何实现一个winform 应用程序在一台电脑上只能打开一个

解决方案 »

  1.   

    在开始的mian函数里面写
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    bool ret;
    Mutex mutexConfig = new Mutex(true, Application.ProductName, out ret);
    if (ret)
    {}
    else
    {
    2次启动
    }
      

  2.   

    Mutex 是你自己编写的吗?????还请详细点,谢谢了
      

  3.   

    using System.Threading;
    系统带的
      

  4.   

    string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; 
    if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0) 
    return true; 
    else return false; true 说明已经运行了 退出就可以
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static int Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (runone())
                Application.Run(new Form2());
            return 1;
            }
            private static System.Threading.Mutex mutex;
            private static bool runone()
            {
                bool one;
                mutex = new System.Threading.Mutex(true, "WindowsApplication2", out   one);
                return one;
            }
        }
    }
      

  6.   


    static class Program
        {
            [DllImport("User32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
            [DllImport("User32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            enum ShowWindowConstants
            {
                SW_HIDE = 0,
                SW_SHOWNORMAL = 1,
                SW_NORMAL = 1,
                SW_SHOWMINIMIZED = 2,
                SW_SHOWMAXIMIZED = 3,
                SW_MAXIMIZE = 3,
                SW_SHOWNOACTIVATE = 4,
                SW_SHOW = 5,
                SW_MINIMIZE = 6,
                SW_SHOWMINNOACTIVE = 7,
                SW_SHOWNA = 8,
                SW_RESTORE = 9,
                SW_SHOWDEFAULT = 10,
                SW_FORCEMINIMIZE = 11,
                SW_MAX = 11
            }
            
            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;
            }
            public static void HandleRunningInstance(Process instance)
            {
                //Make sure the window is not minimized or maximized
                ShowWindowAsync(instance.MainWindowHandle, (int)ShowWindowConstants.SW_RESTORE);            //Set the real intance to foreground window
                SetForegroundWindow(instance.MainWindowHandle);
            }        /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(true);            Process instance = RunningInstance();
                if (instance == null)
                {
                    Application.Run(new MainForm());
                }
                else
                {
                    HandleRunningInstance(instance);
                }        }
        }