Determine whether the program is running and kill the old or new one.

Source: Internet
Author: User

1. Use createmutex

Createmutex (nil, false, 'project ');
Result: = true;
If getlasterror = error_already_exists then
Begin
// Sendmessage (msg_close_win, registerwindowmessage ('project'), 0, 0); // hwnd_broadcast
Close;
Showmessage ('you have the program running allready !! ');
Halt (0 );
Result: = false; // True False
End;

2. Use sendmessage and findwindow (Class Name, caption ),)

Sendmessage (findwindow ('tfrmsimulator', nil), wm_close, 0, 0); // Simulator

3. globalfindatom (this function must be destroyed)

{Search table to see if the program is running}
If globalfindatom ('program _ running') = 0 then
{If it is not running, add information in the table}
Atom: = globaladdatom ('program _ running ')
Else begin
{If the program is running, the system displays information and exits}
Messagedlg ('you have the program running
All ready !! ', Mtwarning, [mbok], 0 );
Halt;
End;

Procedure tform1.formdestroy (Sender: tobject );
Begin
{Remove information from the table so that the program can run again}
Globaldeleteatom (atom );
End;

If you want to disable the previous one and try again, we recommend that you use 2. We recommend that you do not use 3.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.