Search code examples
delphidelphi-xe

How to ensure only a single instance of my application runs?


Is there support in the Delphi XE VCL for ensuring only a single instance of an application is running?

In the past, I've used library code to control a Mutex which has always seemed complicated. As I'm starting a new project in Delphi XE, I wonder if I need to dig up that old code, or if there is support built into XE already? Or is there another easy to apply code that is nice and modern?


Solution

  • I use JCL to do this:

    program MyProgram;
    
    uses
      JclAppInst;
    
    begin
      JclAppInstances.CheckSingleInstance; // Added instance checking
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
    end.
    

    Documentation for this, and the notification scheme, is at the JCL Wiki.