2014-04-28 1 views
0

내가 맥 #의 windorm 응용 프로그램을 작성하고 내가 설치를 완료 할 때마다 나는 그것이 새 창을 열 것입니다 바탕 화면에 바로 가기를 두 번 클릭하는 설치 파일을 생성, 누구나 할 수 않습니다 바탕 화면에서 바로 가기를 두 번 클릭하면 어떻게 원래 창을 열 수 있습니까?

+0

중복 가능성 [이 (http://stackoverflow.com/questions/6486195/ensuring-only-one-application-instance) 중복 [단일 인스턴스 어플리케이션을 생성하는 적절한 방법은 무엇 인? (http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application) –

답변

3

나는 다음과 유사한 뮤텍스 솔루션을 선호합니다. 이미

using System.Threading; 

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

/// <summary> 
/// The main entry point for the application. 
/// </summary> 
[STAThread] 
static void Main() 
{ 
    bool createdNew = true; 
    using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew)) 
    { 
     if (createdNew) 
     { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new MainForm()); 
     } 
     else 
     { 
     Process current = Process.GetCurrentProcess(); 
     foreach (Process process in Process.GetProcessesByName(current.ProcessName)) 
     { 
      if (process.Id != current.Id) 
      { 
       SetForegroundWindow(process.MainWindowHandle); 
       break; 
      } 
     } 
     } 
    } 
} 

로드 이미 실행중인 경우 응용 프로그램 창에 포커스를주는 경우는이 방법으로 응용 프로그램에 다시 초점을 맞추고있다.