就象我们常常打开一个应用程序,会有一个窗口,这时再打开这个应用程序会重复打开窗口,也就是说启动了两个一样的应用程序,有时我们不希望这样,该怎么样避免这种情况,望高手支招,不胜感激

解决方案 »

  1.   

    再程序load里面检测进程表,如果已经存在你的应用程序的进程,就不打开,思路
      

  2.   

    可以在Main函数里使用Process来处理,比如:using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Data;
    using System.Diagnostics;namespace GridApp
    {
    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());
    }
    }
    }
      

  3.   

    定义方法public static bool PrevInstance()
            {
                string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
                if (System.Diagnostics.Process.GetProcessesByName(procName).GetUpperBound(0) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }调用方法static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                    if (Function.PrevInstance())
                    {
                        SCMessage.ShowMsg("已有一个程序的实例正在运行!", 1);
                        Application.Exit();
                    }
                    else
                    {
                        FrmLogon frm = new FrmLogon();
                        frm.Show();
                        Application.Run();
                    }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
            }
      

  4.   

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    这两句请移到if前面
      

  5.   

    把Form f=new Form();   拖到事件外边.
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace Directory_File
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Boolean createdNew; //返回是否赋予了使用线程的互斥体初始所属权
                System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName",out createdNew); //同步基元变量
                if (createdNew) //赋予了线程初始所属权,也就是首次使用互斥体
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1()); 
                    instance.ReleaseMutex();
                }
                else
                {
                    MessageBox.Show("已经启动了一个程序!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    Application.Exit();
                }
            }
        }
    }
      

  7.   

    you can try the singleton design pattern.