천객만래 [千客萬來] (It has an interminable succession of visitors)

'중복실행방지'에 해당되는 글 2건

  1. 2011.09.20 [개발/MFC] 중복실행 방지하기
  2. 2011.09.01 [개발/delphi] 중복 실행 방지
[개발/MFC] 중복실행 방지하기

TCHAR szAppName[] = _T("sbrich");
HANDLE  hMutex  = NULL;
 
bool IsSecondInstance()
{
bool isSecondInstance = false;
hMutex = CreateMutex(NULL, NULL, szAppName);
int iLastError          = GetLastError();
 
if(hMutex && (ERROR_ACCESS_DENIED == iLastError || ERROR_ALREADY_EXISTS == iLastError))
{
ReleaseMutex(hMutex);
hMutex = NULL;
isSecondInstance = true;
}
 
return isSecondInstance;
}


int  main()
{
if(IsSecondInstance())
{
MessageBox(NULL, _T("One Instance Is Already Running."), _T("Information"), 0);
return 0;
}
 
/*App Code Goes Here...*/
 
ReleaseMutex(m_hMutex);
m_hMutex    =   NULL;
return 0;
}

Posted by SB패밀리
델파이로 중복 실행 방지 구현 소스 골라서 쓰자.

===============================================================
#1

program Project;

uses
Forms,
Windows,
Dialogs,
uMain in 'uMain.pas' {frmMain};

var
hMutex : THandle;

{$R *.res}

begin
hMutex := CreateMutex(nil, true, 'MajorProj'); // your mutex
If (hMutex <> 0) and (GetLastError = 0) Then
Begin
Application.Initialize;
Application.MainFormOnTaskbar := true;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
End
Else
Begin
SendMessage(FindWindow('frmMain', '소백'), WM_SBCC, 0, 0);
Application.Terminate;
End;
End.

Const
WM_SBCC = WM_USER + $0001;

// 프로토타입 선언
Procedure WM_WINSBCC(var Message: TMessage); message WM_SBCC;

// ...

procedure TfrmMain.WM_WINSBCC(var Message: TMessage);
Begin
// 여기서 다음 처리
End;

===============================================================
#2
[project1.dpr]
program Project1;  
  
uses  
  Forms, ShellApi,Windows,  
  Unit1 in 'Unit1.pas' {Form1},  
  Unit2 in 'Unit2.pas';  
  
{$R *.res}  
const  
  MUTEX_NAME = 'Mobile100';  
  
var  
  mMutex: THandle;  
  
begin  
  // mutex 열기  
  mMutex := OpenMutex(MUTEX_ALL_ACCESS, False, MUTEX_NAME);  
  
  // mutex 이미 있다면 실행 중지  
  if mMutex <> 0 then Exit;  
  
  // 없으면 mutex만들고 시작  
  mMutex := CreateMutex(nil, True, MUTEX_NAME);  
  
  Application.Initialize;  
  Application.CreateForm(TForm1, Form1);  
  Application.Run;  
end.  

===============================================================
#3

[unit1.pas]

implementation 

var 
 mHandle: THandle; // Mutexhandle 
 Project: string; 

initialization 
 Project := '';          // define your project-name here 

 mHandle := CreateMutex(nil, True, PChar(Project)); 
 if GetLastError = ERROR_ALREADY_EXISTS then begin 
   showMessage('Project ' + Project + ' is still running'); 
   halt;                               // kill the second instance 
 end; 

finalization                            // alt end free the mHandle 
 if mHandle <> 0 then CloseHandle(mHandle) 
end. 

Tested under Win9x and Win2K 





//----------------------------------------------------
#4

procedure TForm1.FormCreate(Sender: TObject); 
var 
 fMutex : THandle; 
begin 
 try 
   { create our mutex }
   fMutex := CreateMutex(nil,
                         FALSE,
                         @Application.Title[1]);

   { test mutex to see if we need to terminate or not } 
   if ( GetLastError() = ERROR_ALREADY_EXISTS ) or 
      ( WaitForSingleObject(fMutex,100) = WAIT_TIMEOUT ) or 
      ( fMutex = 0 ) then 
   begin 

     { keep mainform from flickering } 
     Application.ShowMainForm := FALSE; 

     ShowMessage('You can only have one instance of ['+
                 UpperCase(Application.Title)+
                 '] running at one time.'); 

     { terminate our application } 
     Application.Terminate; 
   end; 

 finally 
   { free our created mutex } 
   ReleaseMutex(fMutex); 
 end; 
end;


=============================================================== 
Posted by SB패밀리