Solved

One instance and Kill program

Posted on 2003-11-10
2
429 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

Technology Partners: 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

724 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