Solved

One instance and Kill program

Posted on 2003-11-10
2
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 84
Delphi 2 77
Firemonkey DbLookupComboBox equivalent ? 2 67
Typecasting TBytes to Integer in Delphi XE8 2 34
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

762 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