728x90
반응형
사용자 실수로 인해 두번이상 실행을 막는 구문입니다.
자세한 내용은 주석을 참고해주세요.
// 클래스 밖에서 선언해주세요.
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static int SW_RESTORE = 9;
// Program.cs 메인함수 내에 추가해주세요. 메인폼 들어가기 직전에 넣어주시면 됩니다.
Process[] pProcess = Process.GetProcessesByName(Assembly.GetEntryAssembly().GetName().Name);
if (pProcess.Length > 1)
{
Process view = pProcess[0];
IntPtr hWndOfPrevInstance = view.MainWindowHandle;
// 만약 핸들이 살아있다면
if (IsIconic(hWndOfPrevInstance))
// 최소화 된 상태를 노말상태로 띄우는 윈도우 메시지를 송신합니다.
ShowWindowAsync(hWndOfPrevInstance, SW_RESTORE);
// 전면에 뛰우는 윈도우 메시지를 송신합니다.
SetForegroundWindow(hWndOfPrevInstance);
// 현재 실행된 프로그램은 종료합니다.
Application.Exit();
return;
}
반응형
'개발 > C#' 카테고리의 다른 글
[2022.09] UTC시간을 현재 시간으로 변환하는 구문 (0) | 2022.09.30 |
---|---|
[2022.09] 버전 관리의 고찰 (1) | 2022.09.29 |
[2022.09] 현재 경로 가져오기 고찰 (0) | 2022.09.26 |
[2022.09] Dapper 클래스 고도화 (0) | 2022.09.22 |
[2022.09] 비프 음을 Stop시킬 때까지 내게 하기 (1) | 2022.09.21 |