Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Prevend double launching of app

Posted on 2002-03-27
10
Medium Priority
?
227 Views
Last Modified: 2010-04-04
What is the code to prevend someone from launching more than 1 instance of my programme ? In simpler terms, it means at any time, only 1 abc.exe is running and not 2.
0
Comment
Question by:tyfing
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:Madmarlin
ID: 6899068
0
 

Author Comment

by:tyfing
ID: 6899161
sorry, i'm a newbie...can u put the code into a button ?
0
 
LVL 3

Accepted Solution

by:
smurff earned 40 total points
ID: 6899477
Try this

implementation

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

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

  mHandle := CreateMutex(nil, True, PChar(Projekt));
  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.


regards
Smurff
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tyfing
ID: 6899508
where is the 'Begin'  ?
0
 
LVL 3

Expert Comment

by:smurff
ID: 6899773
In the actual project1.dpr copy the code, this is out of one I already have.

program Mdiapp;

uses
  Forms,Windows,Dialogs,
  MAIN in 'MAIN.PAS' {MainForm},
  CHILDWIN in 'CHILDWIN.PAS' {MDIChild},
  about in 'about.pas' {AboutBox};

{$R *.RES}

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

begin
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
  Application.CreateForm(TAboutBox, AboutBox);
  Project := 'SmurffsProggy';
  mHandle := CreateMutex(nil, True, PChar(Project));
  if GetLastError = ERROR_ALREADY_EXISTS then begin
   MessageDlg('Application already exists, this instance will now close',mtError,[mbOK],0);
   halt;                               // kill the second instance
  end
  else
  Application.Run;
   if mHandle <> 0 then CloseHandle(mHandle);
end.


To view the project file in the ide goto the delphi menu Project then select view source.

Hope this helps mate,
Regards
Smurff
0
 
LVL 3

Expert Comment

by:smurff
ID: 6899804
Also as a newbie you should know about some sites that would have this kind of code on it so you can save your points on EE. Try these first before asking Qs.


www.torry.net and click on tips
www.delphi3000.com
www.delphifaq.com
www.delphipages.com

and theres lots more and these have links to them.
Just a note, if you know them then just ignore me :)
regards
Smurff
0
 
LVL 3

Expert Comment

by:tongalite
ID: 6900335
Hi
here's a routine from another forum
Thanks to RLord:
T.

 
   
 
How to prevent multiple instances  
One of the best way (and the fastest) is with CreateMutex
--------------------------------

Example:
--------------------------------
program project1;

uses....


begin
// make sure this comes before everything else
CreateMutex(nil, false, ’Someproject’);
if GetLastError = ERROR_ALREADY_EXISTS then begin
SendMessage(HWND_BROADCAST,
RegisterWindowMessage(’Someproject’),
0,
0);
Halt(0);
end;
// and other stuff...
end.  
 
 
0
 
LVL 3

Expert Comment

by:smurff
ID: 6902095
tongalite,

isnt that what Ive put :)

S.
0
 
LVL 3

Expert Comment

by:tongalite
ID: 6902225
Hi smurff..

errrr...well I guess it is now I come to look at your offering... blame sun glare on my screen :-)
T.
0
 
LVL 3

Expert Comment

by:smurff
ID: 6902355
lol the more help the better i suppose :)

Regards
S.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question