2010-02-03 3 views
3

나는 뮤텍스, 의 코드를 통해 갔다 그러나 그것은처럼 나에게 오류를주고있다 : (폐기 BOOL)C# 프로그램에서 뮤텍스 코드를 사용하는 방법은 무엇입니까?

공공 재정의 무효 처분; 더 적합한 방법이 초기화 구성 요소에 발생하는

처분 찾을 수 없습니다, 그래서 그 부분, 를 댓글을 달았지만 내가 직면하고있어 주요 오류가 디자인 뷰 부분에 :

디자이너 26 행에서 코드를 처리 할 수 ​​없습니다 : throw 새 NotImplementedException(); 'InitializeComponent'메서드 내의 코드는 디자이너가 생성하므로 수동으로 수정해서는 안됩니다. 변경 사항을 제거하고 디자이너를 다시 열어보십시오.

form.cs [디자인]을 볼 수 없습니다. 이 문제를 어떻게 해결할 수 있습니까?

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

답변

1

당신이 정말 케이스의 문제 다음 "폐기"메서드를 재정의하려는 경우 - 그것은 Dispose, 아니다 dispose. C#은 대소 문자를 구분합니다.

문제의 나머지 부분에 대해서, 당신이하는 일에 대해 충분한 정보를 제공하지 못했기 때문에 말하기가 어렵습니다. 상황에 대해 더 알려 주면 도움이 될 것입니다. 너 뭐 했니? 정확히? 뮤텍스는 어디로 들어 옵니까?

+0

예 C#은 대/소문자를 구분합니다. Disess inessad를 작성했지만 여전히 해결책을 얻지 못했습니다. im을 사용하여 전체 코드를 얻었습니다. 오류가 발생했습니다. – zoya

+0

프로그램에 직면했습니다. cs : using System; using System.Collections.Generic; using System.Windows.Forms; PU를 사용하는 ; 네임 스페이스 WindowsApplication1 {정적 클래스 프로그램 { [STAThread] 정적 무효 메인() { //이 프로그램이 이미 실행중인 경우, 설정 초점 // 인스턴스 및 종료합니다. if (ProcessUtils.ThisProcessIsAlreadyRunning()) { // "Form1"은 기본 폼의 캡션 (텍스트 속성)입니다. ProcessUtils.SetFocusToPreviousInstance ("Form1");} else {Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault (false); Application.Run (새 Form1()); 내가 그것에 상세 코드를 삽입 할 수 있도록 } } } – zoya

+0

내가 stackover에 전체 코드를 삽입하는 방법을 모른다 PLZ 당신은 * 질문을 수정 새로운 메신저 이 – zoya

관련 문제