C#建立了一个windows程序,如何防止多个程序实例运行?有什么技巧吗?

解决方案 »

  1.   

    可以在Main函数中如下操作:
    [STAThread]
    static void Main()
    {
    System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
    if (ps.Length <= 1)
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
    }
    }
      

  2.   

    收藏学习了 
    借此宝地请教个问题:
    DataGridView控件的每一行记录可否有子行集合(数据来自另外一个相关表),这样形成一个分级,方便浏览.我想效果跟treeview差不多,只是要展示多列记录
      

  3.   

    Application.SetCompatibleTextRenderingDefault(false);
    这句换编译没通过,注释掉后达到同样效果
      

  4.   

    呵呵,我用的VS2005,
    Application.SetCompatibleTextRenderingDefault(false);
    是在VS2005中新增的方法.
    用2003的话就不用了.
      

  5.   

    在main函数中加入如下
    bool bCreatedNew;
    Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );
    if( bCreatedNew )
    Application.Run(new Form1());
      

  6.   

    static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                #region Check the application whether running
                Process process = RunningInstance();
                if (process != null)
                {
                    MessageBox.Show("This application is already running.", ResourceString.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                #endregion                    Application.Run(new MainForm());
            }
            //不允许有两个程序同时启动
            public static Process RunningInstance()
            {
                Process current = Process.GetCurrentProcess();
                Process[] processes = Process.GetProcessesByName(current.ProcessName);
                //遍历正在有相同名字运行的例程 
                foreach (Process process in processes)
                {
                    //忽略现有的例程 
                    if (process.Id != current.Id)
                    {
                        //确保例程从EXE文件运行 
                        if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
                             current.MainModule.FileName)
                        {
                            //返回另一个例程实例 
                            return process;
                        }
                    }
                }
                //没有其它的例程,返回Null 
                return null;
            }
      

  7.   

    我在这篇文章中介绍了三种实现方式,
    http://blog.csdn.net/zhzuo/archive/2006/06/30/857405.aspx
    http://blog.csdn.net/zhzuo/archive/2006/07/04/874745.aspx
      

  8.   

    在main函数中加入如下
    bool bCreatedNew;
    Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );
    if( bCreatedNew )
    Application.Run(new Form1());===============================================================
    我就用这个的,还有一个是检测是否存在同路径下的同进程名的方法,但是效率比较低,而且麻烦
      

  9.   

    回复人:junsheng(月生) ( 一级(初级)) 信誉:99  2007-1-19 15:19:37  得分:0
    在main函数中加入如下
    bool bCreatedNew;
    Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );
    if( bCreatedNew )
    Application.Run(new Form1());------------------->>>>
    以上方法可行,也最簡單,樓主可以使用.
      

  10.   

    using System.Threading;
    using System.Reflection;
    using System.Diagnostics;
                Process[] exe = System.Diagnostics.Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name);
                if (exe.Length > 1)
                {
                    MessageBox.Show("程序已运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }