2010-02-03 59 views
3

我已經通過互斥體, 的代碼走了,但它給我的錯誤:如何在C#程序中使用互斥量代碼?

公衆覆蓋無效的Dispose(BOOL處置);沒有合適的方法發現處置

這在初始化部件發生,所以我評論說部, 但我面臨的主要誤差是在設計視圖部分:

設計者無法處理第26行的代碼:throw new NotImplementedException(); 方法'InitializeComponent'中的代碼由設計者生成,不應手動修改。請刪除所有更改並再次嘗試打開設計器。

我無法查看form.cs [design]。我怎樣才能解決這個問題?

的Program.cs:

using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 
using PU; 

namespace WindowsApplication1 
{ 
    static class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
      // If this program is already running, set focus 
      // to that instance and quit. 
      if (ProcessUtils.ThisProcessIsAlreadyRunning()) 
      { 
       // "Form1" is the caption (Text property) of the main form. 
       ProcessUtils.SetFocusToPreviousInstance("Form1"); 
      } 
      else 
      { 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 
       Application.Run(new Form1()); 
      } 
     } 
    } 
} 

ProcessUtils.cs:

using System; 
using System.Diagnostics; 
using System.Threading; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace PU 
{ 
    /// Summary description for ProcessUtils. 
    public static class ProcessUtils 
    { 
     private static Mutex mutex = null; 

     /// Determine if the current process is already running 
     public static bool ThisProcessIsAlreadyRunning() 
     { 
      // Only want to call this method once, at startup. 
      Debug.Assert(mutex == null); 

      // createdNew needs to be false in .Net 2.0, otherwise, if another instance of 
      // this program is running, the Mutex constructor will block, and then throw 
      // an exception if the other instance is shut down. 
      bool createdNew = false; 

      mutex = new Mutex(false, Application.ProductName, out createdNew); 

      Debug.Assert(mutex != null); 

      return !createdNew; 
     } 

     [DllImport("user32.dll", SetLastError = true)] 
     static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

     [DllImport("user32.dll")] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     static extern bool SetForegroundWindow(IntPtr hWnd); 

     [DllImport("user32.dll")] 
     static extern bool IsIconic(IntPtr hWnd); 

     [DllImport("user32.dll")] 
     static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

     const int SW_RESTORE = 9; 

     [DllImport("user32.dll")] 
     static extern IntPtr GetLastActivePopup(IntPtr hWnd); 

     [DllImport("user32.dll")] 
     static extern bool IsWindowEnabled(IntPtr hWnd); 

     /// Set focus to the previous instance of the specified program. 
     public static void SetFocusToPreviousInstance(string windowCaption) 
     { 
      // Look for previous instance of this program. 
      IntPtr hWnd = FindWindow(null, windowCaption); 

      // If a previous instance of this program was found... 
      if (hWnd != null) 
      { 
       // Is it displaying a popup window? 
       IntPtr hPopupWnd = GetLastActivePopup(hWnd); 

       // If so, set focus to the popup window. Otherwise set focus 
       // to the program's main window. 
       if (hPopupWnd != null && IsWindowEnabled(hPopupWnd)) 
       { 
        hWnd = hPopupWnd; 
       } 

       SetForegroundWindow(hWnd); 

       // If program is minimized, restore it. 
       if (IsIconic(hWnd)) 
       { 
        ShowWindow(hWnd, SW_RESTORE); 
       } 
      } 
     } 
    } 
} 
+0

如果您無法打開form.cs的設計視圖,請粘貼該類的代碼,這可能有助於解決您的查詢問題。 – shahjapan 2010-02-06 06:38:51

回答

1

如果你真的想覆蓋 「處理」 方法則是外殼的問題 - 這是Dispose,不dispose。 C#區分大小寫。

至於其餘的問題,很難說,因爲你真的沒有提供足夠的信息,你正在做什麼。如果您首先告訴我們更多關於您的情況,這將有所幫助。你究竟做了什麼?互斥體進入這個地方在哪裏?

+0

是的,我知道C#是區分大小寫我已經寫了Dispose insead的處置,但仍然沒有得到解決方案 即時通訊給我你已經試過的整個代碼只是通過它,然後期待在它面臨的錯誤 – zoya 2010-02-03 08:56:02

+0

程序。 cs: 使用系統; using System.Collections.Generic;使用System.Windows.Forms的 ;使用PU的 ; 命名空間WindowsApplication1 {靜態類節目 { [STAThread]靜態無效的主要(){// 如果該程序已經運行,將焦點設置 //到該實例並退出。 if(ProcessUtils.ThisProcessIsAlreadyRunning())//「Form1」是主窗體的標題(Text屬性)。 ProcessUtils.SetFocusToPreviousInstance(「Form1」);} else {Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } – zoya 2010-02-03 09:01:34

+0

我不知道如何把整個代碼放在stackover plz告訴我的方法,以便我可以把詳細代碼放在上面...... 即時消息新到stackover – zoya 2010-02-03 09:18:05