본문 바로가기
Delphi, RadStudio

[개발/delphi] 중복 실행 방지

by SB리치퍼슨 2011. 9. 1.
델파이로 중복 실행 방지 구현 소스 골라서 쓰자.

===============================================================
#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;


=============================================================== 
반응형

댓글