Notice
Recent Posts
Recent Comments
관리 메뉴

안까먹을라고 쓰는 블로그

[C#] 프로세스 중복체크 && 기 실행된 프로세스 최상위로 올리기 본문

Language/C#

[C#] 프로세스 중복체크 && 기 실행된 프로세스 최상위로 올리기

YawnsDuzin 2018. 10. 17. 15:32

 

반응형

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

using Default;
using GV = Default.clsGlobalVal;

using System.Diagnostics;                                   // 프로세스 사용
using System.Runtime.InteropServices;                       // DLL Import 사용
using System.Threading;

namespace DataSave_HopperScale
{
    static class Program
    {
        // 이미 실행중이면, 포커스
        [DllImport("user32")]
        private static extern bool SetForegroundWindow(IntPtr handle);
        // 이미 실행중이면, 보이게
        [DllImport("User32")]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        // 이미 실행중이면, 맨앞으로
        [DllImport("User32")]
        private static extern void BringWindowToTop(IntPtr hwnd);
        // 중복실행을 방지함
        [DllImport("User32")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        private static Mutex m_gm;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                // XML Initial
                clsXML XML = new clsXML("Config", Application.StartupPath);
                GV.ProcessName = XML.Get_data_Read("Default", "ProcessName");

                bool bCreated = false;
                m_gm = new Mutex(true, GV.ProcessName, out bCreated);

                // 프로그램 중복실행 체크
                // 실행이 안되어 있으면,,
                if (bCreated == true)
                {
                    Application.EnableVisualStyles();
                    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());
            }
        }
    }
}

반응형

'Language > C#' 카테고리의 다른 글

[C#] KAKAO API 연동하기  (0) 2018.12.06
[DevExpress] ChartControl  (0) 2018.11.13
[DevExpress] Chart Control  (1) 2018.10.02
[DevExpress] GridView Header 정렬 / 폰트 설정  (0) 2018.10.02
[DevExpress] GridView Column CheckBox로 변환  (0) 2018.10.02
Comments