ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#] 프로세스 중복체크 및 실행된 프로세스 최상위 표시
    취미생활/C# 2019. 11. 29. 08:34
    반응형

    안녕하세요

     

    코딩하는남자에 코딩연습생입니다~

     

    간혹 프로젝트를 진행하다 보면 두개의 프로젝트를 하나의 솔루션에 합쳐서 개발해야 할 때가 발생한다

     

    뭐 프로젝트 두개 실행시키는거야 어렵지 않은데...

     

    한쪽의 프로젝트에서 나머지 한쪽을 제어하는듯(?)이 보여 주고 싶을 때가 있다

     

    예를들면 1번 프로젝트가 실행되고 나면 자연스럽게(?) 두번째 프로젝트가 실행된다던지

     

    1번 프로젝트를 종료하면 자연스럽게(?) 두번째 프로젝트가 종료되는 이런 간단한 조작이 필요할 때가 있다

     

    저의 경우 ClickOnce를 주로 사용하여 배포를 하는데 이 경우 실행된 프로세스를 통해 다음과 같은

     

    동작을 구현할수 있다

     

    이게 좋은 방법인지 아닌지는 잘 모르겠다 하지만 필요할때가 있다

     

     

    [C# Source Code]

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Windows.Forms;
    
    using System.Diagnostics;
    
    using System.Runtime.InteropServices;
    
    using System.Threading;
    namespace Ex_Process_Ctl
    
    {
    
      static class Program
    
      {
    
         //이미 실행중이면 포커스
    
         [DllImport("user32.dll")]
    
         private static extern bool SerForegroundWindows(IntPtr handle);
    
         //이미 실행중일때 활성화
    
         [DllImport("user32.dll")]
    
         private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    
         //이미 실행중일때 최상위 표시
    
         [DllImport("user32.dll")]
    
         private static extern void BringWindowToTop(IntPtr hwnd);
    
         //중복실행 방지
    
         [DllImport("user32.dll")]
    
         private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
    
    
    
        ///<summary>
    
       ///The main entry point for the application.
    
       ///</summary>
    
       [STAThread]
    
       static void Main()
    
       {
    
          try
    
          {
    
             //프로그램 중복 체크
    
             if(bCreated == true)
    
             {
    
                Application.EnbleVisualStyles();
    
                Application.SetCompatibleTextRenderingDefault(false);
    
                Application.Run(new frmMain());
    
             }
    
             else
    
             {
    
                foreach(Process process in Process.GetProcesses())
    
                {
    
                   if(process.ProcessName == GV.ProcessName)
    
                   {
    
                      ShowWindow(process.MainWindowHandle, 5);
    
                      BringwindowToTop(process.MainWindowHandle);
    
                      SetForegroundWindow(process.MainWindowHandle);
    
                   }
    
                }
    
             }
    
          }
    
          catch(Exception Ex)
    
          {
    
              MessageBox.Show(Ex.ToString());
    
          }
    
       }
    
      }
    
    }

     

    반응형
Designed by Tistory.