如题。还有如何判断程序是否已经运行,只运行一个应用程序?

解决方案 »

  1.   

    3gold(新丁)能否说详细点?我的应用程序如果是运行在不允许修改注册表的机器中呢?
      

  2.   

    楼上的说的  Singleton 模式是可以的。
      

  3.   

    用Singleton 模式?
    是说在窗体的构造函数中定义一个静态变量和一个私有的构造函数?不好意思,本人是个新手对设计模式这方面有点薄弱。请各位指点。
      

  4.   

    最好用个实例解释一下,3Q。例如我在FormMain中点击按钮要求只显示一个FormUserInfo窗体,
    如果用Singleton 模式,在FormUserInfo中应该做何设置?
    点击时又要怎么做?
      

  5.   

    最方便的方法就是自己维护一个全局的窗体列表,打开新窗口的时候根据这个列表判断是否已打开。
    关于如何判断程序已运行,只运行一个应用程序,使用下述语句:
    static void Main() 
    {
    System.Diagnostics.Process[] ProcessList = System.Diagnostics.Process.GetProcessesByName("RemotingTest");//填应用程序名
    if (ProcessList.Length > 1)
    {
    MessageBox.Show("已有相同软件在运行!");
    }
    else
    Application.Run(new Form1());
    }
      

  6.   

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Reflection; public class OneInstnace
     { 
     [STAThread]
     public static void Main()
     {
     //Get the running instance.
     Process instance = RunningInstance();
     if (instance == null)
     {
     //There isn't another instance, show our form.
     Application.Run (new Form());
     }
     else
     {
     //There is another instance of this process.
     HandleRunningInstance(instance);
     }
     }
     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 , WS_SHOWNORMAL); //Set the real intance to foreground window
     SetForegroundWindow (instance.MainWindowHandle);
     } [DllImport("User32.dll")] private static extern bool ShowWindowAsync(
     IntPtr hWnd, int cmdShow);
     [DllImport("User32.dll")] private static extern bool
     SetForegroundWindow(IntPtr hWnd);
     private const int WS_SHOWNORMAL = 1;
     }
    ×××××××××××××××××××××××××××××××××××××××
    static void Main() 
    {
    //判断之前是否已经打开了相同的进程
    //new Bestzone.MIS.Report.F1BookContainer();
    bool alreadyExist = false;
    try
    {
    System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess();
    System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
    foreach(System.Diagnostics.Process p in ps) 
    {
    if(p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id) 
    {
    alreadyExist = true;
    }
    }
    }
    catch(System.PlatformNotSupportedException ex){ex.ToString();}
    catch(System.InvalidOperationException ex){ex.ToString();}//如果已经存在,则放弃本进程
    if(alreadyExist) 
    {
    return;
    }Application.Run(new MainForm());
    }
      

  7.   

    单模式在这里不是很适用,建议使用如下方法:public System.Threading.Mutex  SingleMutex ;
    public  Form1()
    {
       SingleMutex = new Mutex(false, "SINGLE_INSTANCE_MUTEX");
       if (!SingleMutex.WaitOne(0,false))
        {
    SingleMutex.Close();
    SingleMutex=null;
            return ;
         }
    .....}

    protected override void Dispose( bool disposing )
    {
       if( disposing )
    {
     if (this.SingleMutex !=null)
    {
    this.SingleMutex.ReleaseMutex();
    this.SingleMutex.Close();
    this.SingleMutex=null;
    }
    .....
       }

    }
    具体使用参见.net sdk :
    ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfSystemThreadingMutexMembersTopic.htm
      

  8.   

    static void Main() 
    {
    if (IsSingleProcess ())
    {


    Application.Run(new Form1 ());


    }



    }
    // 是否只有一个进程
    private static bool IsSingleProcess ()
    {

    int i = System.Diagnostics.Process.GetProcessesByName (System.Diagnostics.Process.GetCurrentProcess ().ProcessName).Length ;
    if (i == 1)
    return true;
    else
    return false ; }
      

  9.   

    System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce");
    bool Running = !mutex.WaitOne(0, false);
    if (! Running)
       Application.Run(new frm());
    else
       MessageBox.Show("应用程序已启动!");
      

  10.   

    在实例化frmMain的时候永远只有一个
    即不让实例化frmMain,只是提供一个方法来返回frmMain的一个静态实例
    这样就保证了所有的child的父亲是同一个
    当实例化子窗口的时候,循环frmMain所有的打开子窗口,如果已经打开了一个,那么就
    不用再打开该子窗口了
      

  11.   

    losthold(呵呵)的方法可行,试过了。