Solved

One instance and Kill program

Posted on 2003-11-10
2
421 Views
Last Modified: 2010-04-05
1) How can i make the program only run one (one instance) at a time ?
2) How can i kill the program if the program can not close normally ?
I had try the release(). Although the form was closed, i still
can see there is the process in Process Manager.
0
Comment
Question by:expert173173
2 Comments
 
LVL 4

Accepted Solution

by:
BedouinDN earned 100 total points
ID: 9719616
For single instance you can use a mutex.
Place the following in the source of your project (DPR) before the Application.Initialize part :-)
//---------------------------------
var
MutexHandle: THandle;

begin
MutexHandle := CreateMutex(nil, TRUE, App Name Here);
        if MutexHandle <> 0 then
        begin
                if GetLastError = ERROR_ALREADY_EXISTS then
                begin
                CloseHandle(MutexHandle);
                Halt; // 'Halt' Is the actual one that prevents a second
                end;
        end;
//-------------------------------------------------------------------------------

As for closing the application - When you close the form named as the main form, the application should terminate.
Failing this, you can try calling the Application.Terminate method instead of calling close however this is a bit nasty.
0
 
LVL 2

Expert Comment

by:SaLz
ID: 9719884
if your wondering what you should put in the app name it is the Class as in what you have named your form
you could put PChar((form1)) if you get errors
MutexHandle := CreateMutex(nil, TRUE, PChar(form1));
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now