就是运行第一个实例程序后到托盘,再运行同一个程序的第二个实例,不会再运行这个实例程序,只是把第一个实例主界面显示出来。
求助 怎么实现。

解决方案 »

  1.   

    思路:1,程序运行,判断是否为唯一进程
    2,如果是,缩小到托盘
    3,如果不是,向另一线程发送打开主界面的消息,并且关闭自身发送的方法有很多,API的SENDMESSAGE,或者自定义程序通讯(WINSOCK,写文件,等等)
      

  2.   

    [DllImport("user32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            [DllImport("user32.dll")]
            private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
            [DllImport("user32.dll")]
            private static extern bool IsIconic(IntPtr hWnd);
            private const int SW_RESTORE = 9;        public void RaiseOtherProcess()
            {
                Process proc = Process.GetCurrentProcess();
                Process.GetProcesses();
                foreach (Process otherProc in
                    Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
                {
                    //ignore "this" process
                    if (proc.Id != otherProc.Id)
                    {
                        // Found a "same named process".
                        // Assume it is the one we want brought to the foreground.
                        // Use the Win32 API to bring it to the foreground.
                        IntPtr hWnd = otherProc.MainWindowHandle;
                        if (IsIconic(hWnd))
                        {
                            ShowWindowAsync(hWnd, 9);
                        }
                        SetForegroundWindow(hWnd);
                        break;
                    }
                }
            }
    还是用mutex实现互斥。
    当互斥发生时调用以上代码,实现:
    2.如果程序已经存在,且最小化,则还原那个程序。
    3.如果程序已经存在,且不是最小化(最大化或正常状态),则显示(注意:不是还原!)那个程序。
    其实你主要是不知道可以用这个方法:SetForegroundWindow(hWnd);
    来显示程序。 
    http://zhidao.baidu.com/question/116698008.html
      

  3.   

    再结合这段代码,应该没什么问题了。private static   void GetSingleThread() 
       {          
        string name = Process.GetCurrentProcess().ProcessName; 
        int id = Process.GetCurrentProcess().Id; 
        Process[] prc = Process.GetProcesses();
        foreach(Process pr in prc)
        { 
         if ((name == pr.ProcessName) && (pr.Id != id)) 
         {      
          MessageBox.Show("对不起,本地已经有系统正在运行!\n.","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); 
          System.Environment.Exit(0);
         } 
        }