| |

VerySource

 Forgot password?
 Register
Search
View: 1419|Reply: 9

How to prevent Delphi programs from executing repeatedly?

[Copy link]

2

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-15 03:00:01
| Show all posts |Read mode
How to prevent Delphi programs from executing repeatedly?
I searched online, there are many versions, I don't know which one is better. My current program is an MDI window type. At first there was only one login window. After the login information was verified, the corresponding permissions were obtained to display the main window and its menu.
Do you have any practical mature methods that are already in use? Please enlighten me, thank you!
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-23 22:05:55
| Show all posts
Mutual exclusion! !! !!
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-19 15:15:01
| Show all posts
"Project"-> "View Source"

program Project1;

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

{$ R * .res}

begin
  // Prevent the program from running multiple times
  CreateMutex (nil, false, PChar ('string identifying the program'));
  if GetLastError = ERROR_ALREADY_EXISTS then
    exit;

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

Notice the Windows unit
Reply

Use magic Report

0

Threads

53

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-2 12:30:01
| Show all posts
CreateMutex (nil, false, 'Project_Client.exe');
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
    SendMessage (HWND_BROADCAST, RegisterWindowMessage ('Project_Client.exe'), 0, 0);
    ShowMessage ('The program is already running!');
    Halt (0);
  end;
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-10 19:30:01
| Show all posts
These are enough for you, as long as you can do it.
Reply

Use magic Report

2

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-6-3 12:30:02
| Show all posts
Thank you, the above problem is solved, but only the prompt message comes out, and the application that has been minimized or hidden behind other windows cannot be called out. How do I deal with it? Thank you!
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-8 15:00:01
| Show all posts
CreateMutex method
===
After the program does not exit normally, will it run again?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-13 12:15:02
| Show all posts
Improvements to prevent multiple application instances from appearing at the same time

//==================================================== ==============================
// Unit Name: MultInst
// Purpose: Solve the problem of multi-instance application
// History:
//==================================================== ==============================

//==================================================== ==============================
// work process
// The program runs first to replace the original message processing process, and then broadcast a message.
// If there are other instances running, the broadcast message will be sent back to the sending program, and its own handle will be returned
// The sending program receives this message, activates the program that received the message, and then closes itself
//==================================================== ==============================
unit MultInst;

interface

uses
  Windows, Messages, SysUtils, Classes, Forms;

implementation

const
  STR_UNIQUE ='{2BE6D96E-827F-4BF9-B33E-8740412CDE96}';
  MI_ACTIVEAPP = 1; //Activate the application
  MI_GETHANDLE = 2; //Get handle

var
  iMessageID: Integer;
  OldWProc: TFNWndProc;
  MutHandle: THandle;
  BSMRecipients: DWORD;

function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint):
  Longint; stdcall;
begin
  Result := 0;
  if Msg = iMessageID then
  begin
    case wParam of
      MI_ACTIVEAPP: //Activate the application
        if lParam<>0 then
        begin
          //Activate the previous instance of the received message
          //Why activate in another program?
          //Because SetForegroundWindow does not bring the form to the front in the same process
          if IsIconic(lParam) then
            OpenIcon(lParam)
          else
            SetForegroundWindow(lParam);
          //Terminate this instance
          Application.Terminate;
        end;
      MI_GETHANDLE: //Get the program handle
        begin
          PostMessage(HWND(lParam), iMessageID, MI_ACTIVEAPP,
            Application.Handle);
        end;
    end;
  end
  else
    Result := CallWindowProc(OldWProc, Handle, Msg, wParam, lParam);
end;

procedure InitInstance;
begin
  //Replace the application's message processing
  OldWProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
    Longint(@NewWndProc)));

  //Open the mutex object
  MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, STR_UNIQUE);
  if MutHandle = 0 then
  begin
    //Create a mutex object
    MutHandle := CreateMutex(nil, False, STR_UNIQUE);
  end
  else begin
    Application.ShowMainForm := False;
    //There is already a program instance, the broadcast message gets the instance handle
    BSMRecipients := BSM_APPLICATIONS;
    BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
        @BSMRecipients, iMessageID, MI_GETHANDLE, Application.Handle);
  end;
end;

initialization
  //Registration message
  iMessageID := RegisterWindowMessage(STR_UNIQUE);
  InitInstance;

finalization
  //Restore message processing
  if OldWProc <> Nil then
    SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(OldWProc));

  //Close the mutex object
  if MutHandle <> 0 then CloseHandle(MutHandle);

end.



//The method of use is very simple, just add this unit to the project.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-18 21:30:01
| Show all posts
There seems to be such a component in the jedi library, just drag it to the form.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Japan

Post time: 2020-7-19 23:45:01
| Show all posts
When you are in other programs
Change it
STR_UNIQUE ='{2BE6D96E-827F-4BF9-B33E-8740412CDE96}';
That's it.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list