onlyone TacOnlyOne component
Hierarchy Properties Methods Events
Return to Introduction  Previous page  Next page
Overview
The acOnlyOne component detects already running instances of the application and disallow to start the second copy of the same program.  
 
After detecting of the second application instance, acOnlyOne will automatically terminate program execution and switch the tasks to previous program instance (if SwitchToFirst property is True). If you wish to take some specific actions if another application instance detected (application already running) — write OnAlreadyExists event handler.  
 


new! The acOnlyOne unit now exports several functions which can be used outside of the component, in the initialization section of your application (*.DPR unit), so you can close second application instance and switch the input focus to previous, before the second instance will create any form.  
 
Here is an example (procedures exported from acOnlyOne unit marked with red color):  
program YourAppTitle;  
 
uses  
  acOnlyOne,  
  acUtils,  
  Main in 'Main.pas' {MainForm};  
 
{$R *.res}  
 
begin  
  if HasParamStr('/close'then  
   begin  
    CloseFirstAppInstance;  
    Exit;  
   end;  
 
  // The Application.Title should be initialized before checking whether the application instance exists  
  Application.Initialize;  
  Application.Title := 'YourAppTitle';  
 
  if IsAppInstanceAlreadyExists then  
   begin  
    InfoBox('The application already running!');  
    SwitchToFirstAppInstance;  
    Exit;  
   end;  
 
  Application.CreateForm(TMainForm, MainForm);  
  Application.Run;  
end.