For Newbie

Tuesday, 15 November 2011

app PrevInstance delphi



Tricks to outsmart the application at the same time only one instance is allowed (run).

Function Declaration:
PrevInstance function (AppID: String): Boolean;
begin
   / / Handle Applications When you first run ...
   if WaitForSingleObject (CreateMutex (nil, False, PChar (AppID)), 0) <> wait_timeout then
   Result: = False else Result: = True;
end;

The function is similar to the function App.PrevInstance owned by Visual Basic. Add functionality to your project source (Select menu Project - View Source).

implementation:
begin
   if PrevInstance ('X Project V.1') then
   begin
     / / Command if the application is Running ...
     MessageBox (Application.Handle, 'Application Running!',
               'Info', MB_ICONWARNING);
     Application.Terminate;
   end
   else
   begin
     Application.Initialize;
     Application.Title: = 'X Project';
     Application.CreateForm (TForm1, Form1);
     Application.Run;
   end;
end.

For the AppID parameter, can be tailored to the wishes. For instance identifier of the application form of the name. Exe file it:
if PrevInstance (Application.ExeName) then / / etc .....

As proof: compile and try to run the program output (not to be terminated first) and then run it again.
Do not forget to add the clause uses the Windows unit.

Share on Facebook
Share on Twitter
Share on Google+

Related : app PrevInstance delphi