1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/od/windowsshellapi/l/aa100703a.htm

was emailed to:

Thanks for sharing About.com with others!

Controlling the number of application instances
Page 1: Where it all starts...
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Page 2: Handling previous instances of an application
• Page 3: Limiting multiple instances. Restoring last instance.
• Page 4: Full CODE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Delphi Project File (DPR)
• Windows API and Delphi

By default, any Windows application can be executed by a user multiple times - having all instances (copies) running at the same time. This is perfectly normal (and expected) in a multitasking environment, such as in Windows.

However, there are many situations and reasons why you would need to make sure that only one instance of your application executes. For example, if your application is relying on the Registry, having multiple instances of the same application reading and writing registry data files may result in data corruption. Another example includes working with databases or databases made from files of records.

In this article you'll learn how to "run-once" enable a Delphi application that can check for its previous (running) instance. Along the process, several techniques of implementing such a check will be discussed; as well as how to bring your already running application to the foreground, if a user tries to run it "one more time". By the end of the article you'll have a copy-to-go code to control the behavior of your application's multiple instances: with the option to limit the number of running instances.

Where it all starts...
As you already know, every Delphi project (application) can have multiple forms (with associated units), one or more code units, and a single project file. The project file is the central point for the project's source code. A project unit contains directions for building an application, as well as references to all the forms and units used by the project.

Hey-ho let's go
It's the right time to start Delphi ... a new project is created along with an empty form. To see the source of the project file choose Project - View Source from the main Delphi IDE menu. This is what you should see:

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

If you are not familiar with the project source file, please first read the "Understanding and managing the Project file" article.

Quick recap:
The Application.Run statement starts your application. The CreateForm method of the global Application object creates the form specified in its argument. The main form of your application is the first form created in the list. When the main form closes, the application terminates. In the above code, the Form1 (class: TForm1) is the only form in the project, and therefore the main form of the application.

Conclusion:
If you want to make sure your application gets executed only once, this is the place to add code!

There can be only one!
Now, we'll explore several ways of implementing the "Am I running already" check.

First, what you need to do, in order to stop the second copy of your application from running, is to "ask" the system whether there is an instance of your program already running. If the system (Windows) responds "yes", then your task is to disable the second copy from being executed.

Second, if the system responded "yes", and the user is trying to run the program again, you'll probably want the first copy to become active - you'll need to bring your already running application to the foreground.

Great. But how does an application ask the system "Am I already running"? You'll find the answer to this question on the next page...

p.s.
As you will see from the rest of the article, the first part is rather simple. The second part, well, that can be complicated.

Next page > Handling previous instances of an application > Page 1, 2, 3, 4

©2014 About.com. All rights reserved.