C# – 중복 실행 방지

#1

private Object theLock = new Object();
private bool checkAliveProcess()
{
    int processcount = 0;
    Process[] procs;
    procs = Process.GetProcesses();
    foreach (Process aProc in procs)
    {
        if (aProc.ProcessName.ToString().Equals("iMessenger"))
        {
            processcount++;
        }
    }
    if (processcount == 1)
    {
        Application.Run(new ServerMain());
    }
    else
    {
        MessageBox.Show("이미 서버가 실행중입니다.", "정보알림", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return;
    }

}

 

#2

private Object theLock = new Object();
using System.Runtime.InteropServices;
 
[DllImportAttribute("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(string clsName, string wndName);
 
// 프로그램이 먼저 실행중이 아니면 0을 리턴
int a=FindWindow(null,"폼이름");

 

#3

private Object theLock = new Object();
[STAThread]
static void Main() 
{
    int processcount=0;
    Process[] procs;
    procs = Process.GetProcesses();
     
    foreach(Process aProc in procs)
    {
        if(aProc.ProcessName.ToString().Equals("프로세스 네임"))
        {
            processcount++;
        }
    }

    if(processcount == 1)
        Application.Run(new Form1());
    else
    {
        MessageBox.Show("벌써 하나 실행 되고 있지롱");
        return;
    }
}

 

#4

private Object theLock = new Object();
private void Form1_Load(object sender, System.EventArgs e)
{
    bool CreateNew;
    Mutex m=new Mutex(true,"FIRST",out CreateNew);
    if(CreateNew)
    {
        MessageBox.Show("첨 실행");
    }
    else
    {
        MessageBox.Show("두번째 실행->종료");
        Application.Exit();
    }
 
}

 

 

 

보기만 해도 아픈 짤방
사용자 삽입 이미지

2개의 댓글

댓글 남기기