2012-08-12 49 views
3

我有這個問題嗎?! 我使用這種方式只運行一個程序實例。 它做得很好,但是當我在其他應用程序中使用這種方式。 當我通過桌面上的快捷方式運行其中的一個程序時,這兩個程序都會調用並顯示在桌面上。 注意:這兩個程序都在windows系統下運行試試。運行一個程序實例

static bool ok; 
    static Mutex mutex = new Mutex(true, "{123Newsoft-Cleaner Portable Program123}",out ok); 

    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 

    static void Main() 
    { 
     //Application.EnableVisualStyles(); 
     //Application.SetCompatibleTextRenderingDefault(false); 
     //Application.Run(new Form1()); 

     if (mutex.WaitOne(TimeSpan.Zero, true)) 
     { 

      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 


      var mainForm = new Form1c(); 

      try 
      {          

        mainForm.Visible = false; 


        mainForm.WindowState = FormWindowState.Normal;     

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 

      Application.Run(mainForm);    

     } 
     else 
     { 

      NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); 

     } 

// ----------------的主要形式

protected override void WndProc(ref Message M_C) 
    { 

     if (M_C.Msg == NativeMethods.WM_SHOWME) 
     {    
      ShowMe(); 
     } 
     base.WndProc(ref M_C); 
    } 

    //************* 
    private void ShowMe() 
    { 

     if (WindowState == FormWindowState.Minimized) 
     { 
      Show(); 
      WindowState = FormWindowState.Normal; 
     } 

     // get our current "TopMost" value (ours will always be false though) 
     bool top = TopMost; 
     // make our form jump to the top of everything 
     TopMost = true; 
     // set it back to whatever it was 
     TopMost = top; 

    } 

回答

8

這早已由.NET Framework的支持。你想使用WindowsFormsApplicationBase類。將IsSingleInstance屬性設置爲true。您可以重寫OnStartupNextInstance方法以在另一個實例啓動時執行任何您喜歡的操作。就像恢復第一個實例的窗口一樣。重寫你的Program.cs文件,看起來像這樣:

using System; 
using System.Windows.Forms; 
using Microsoft.VisualBasic.ApplicationServices; // Add reference to Microsoft.VisualBasic 

namespace WindowsFormsApplication1 { 
    class Program : WindowsFormsApplicationBase { 
     [STAThread] 
     static void Main(string[] args) { 
      var app = new Program(); 
      app.Run(args); 
     } 
     public Program() { 
      this.IsSingleInstance = true; 
      this.EnableVisualStyles = true; 
      this.MainForm = new Form1(); 
     } 
     protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) { 
      if (this.MainForm.WindowState == FormWindowState.Minimized) this.MainForm.WindowState = FormWindowState.Normal; 
      this.MainForm.Activate(); 
     } 
    } 
} 
+0

感謝:)我使用.net 3.5這不工作:Microsoft.VisualBasic.ApplicationServices; – pepero 2012-08-12 17:40:44

+1

這適用於3.5。看看評論,使用Project +添加引用。 – 2012-08-12 17:42:22

+0

mersi.its work.unorable有老問題。當運行一個應用程序時,同時顯示:( – pepero 2012-08-13 02:23:30

1

要添加到什麼漢斯帕桑特寫的,我補充說,處理恢復窗口,並激活它在主窗體上一個額外的方法。這是爲了包裝表單的調用所需條件。

所以在窗體上添加方法是:

/// <summary> 
/// Recovers this instance of the form. 
/// </summary> 
public void RestoreFromTray() 
{ 
    if(this.InvokeRequired) 
    { 
     this.Invoke(new Action(RestoreFromTray)); 
     return; 
    } 
    this.Visible = true; 
    this.WindowState = FormWindowState.Normal; 
    this.Activate(); 
} 

然後在漢斯的方法,我改變了超越簡單:

protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) 
{ 
    ((formClassName)this.MainForm).RestoreFromTray(); 
} 

哪裏formClassName是形式的類名。

+0

如果你需要這個,那麼你正在做的事情**非常* *錯誤。在某些需要[此類調試]的下雨天將會掛起您的應用程序的錯誤類型(https://blogs.msdn.microsoft.com/dsui_team/2012/10/31/debugging-windows- *從不*在工作線程上顯示UI,SystemEvents類會吃掉你的肝臟 – 2016-08-16 19:26:36

+0

我想我錯過了一些東西 - 我添加了InvokeRequired測試來確保窗口被恢復在正確的t hread。我哪裏弄錯了? – 2016-08-16 19:50:41

相關問題