How can I prevent the second instance of my application to to be run using delphi

Hello . I'm going to prevent the other instanses of my application to be run , while the first instanse is still running , Please offer me a sample code .
Iman_DavoodianAsked:
Who is Participating?
 
Steven-FernandezCommented:
Try this:

Here is an example:

Uses: Windows, Dialogs

Insert code in the projects source (.dpr file)

var
  MutexHandle : THandle;
begin
  MutexHandle := CreateMutex(nil, True, 'AppMutex');
  if MutexHandle <> 0 then
  begin
    if GetLastError = ERROR_ALREADY_EXISTS then
    begin
      Showmessage('An Instance of this application is already running.');
      CloseHandle(MutexHandle);
      Halt;
    end;
  end;

{Application.Initialise, run etc... follows from here}

end.




0
 
Pierre CorneliusCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.