求C#如何判断我的程序已经有一个实例正在运行了。因我不希望再运行第二个实例了。

解决方案 »

  1.   

      Process[] allProcess = Process.GetProcesses();//所有进程
                    foreach (Process p in allProcess)
                    {                    if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())//mainAppExe你的进程名
                        {                       
                            ...
                        }
                    }
      

  2.   

        static void Main()
            {
                // get the name of our process
                string proc = Process.GetCurrentProcess().ProcessName;
                // get the list of all processes by that name
                Process[] processes = Process.GetProcessesByName(proc);
                // if there is more than one process
                if (processes.Length > 1)
                {
                    MessageBox.Show("程序已经在运行中","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    Application.Run(new Form1());
                }
            }
      

  3.   

    直接判断对象是否等于null,如果是null的话就说明没有实例,然后你在创建实例,如果不等于null,就不要在创建实例了;
      

  4.   

    这样可以实现,而且保证可行:
    导入命名空间:
    再Program类中
    using System.Windows.Forms;
    using System.Threading;    static class Program
        {
            private static Mutex mutex;
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                mutex = new Mutex(true, "OnlyRun");
                if (mutex.WaitOne(0, false))
                {
                    Application.Run(new Form1());
                }
                else
                {
                    MessageBox.Show("程序已经运行!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    Application.Exit();
                }
            }
        }
      

  5.   

    好办法,学习了。
    我以前用的是一个全局变量,如果程序运行,这个变量为NULL,
    只有在这个变量为NULL时,才实例化。
      

  6.   


     if (BI.KillMore())
                {
                    MessageBox.Show(BI.strError);
             
                }
                else
                {
                    Application.Run(new Login());
                } public static bool KillMore()
            {
                int length = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
                return length > 1;
            }//没想到刚开始学的一点小东西还有用途
      

  7.   

    可以参考以下:
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
                {
                    return;
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
      

  8.   

    意思很清楚...互斥体,进程间同步...去看MSDN...
      

  9.   

    这两个函数的实际意义是什么,去掉不可以吗? 
               Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
     
      

  10.   

    可以去掉...但是...去掉第一句...你的程序就不能使用Windows的主题和样式,在XP和Vista中会很难看...去掉第二句...你的程序不使用GDI+,UI渲染会慢一些,可以与.NET 1.x程序媲美...
      

  11.   

    互拆体
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;namespace FallingGold
    {
        static class Program
        {
            private static Mutex mutex;
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                mutex = new Mutex(true, "一个");            
                //保证只有一个实例运行
                if (mutex.WaitOne(0, false))
                {
                    Application.Run(new MaimForm());
                }
            }
        }
    }
      

  12.   

    我习惯用的代码:        static void Main()
            {
                bool createNew;
                string name = Process.GetCurrentProcess().ProcessName + "Mutex";
                using (Mutex mtx = new Mutex(true, name, out createNew))
                {
                    if (!createNew) return;
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                }
            }