How to only let it run multiple times a program

xiaoxiao2021-03-06  65

/ (1) Project file Test.dpr // Program serialget;

Uses form, umain in 'umain.pas' {frmmain}, ulogin in 'uform2.pas' {form2}, udatamodule in 'udatamodule.pas' {datamodule1: tdataModule},

{$ R * .res}

Begin Application.initialize;

if CreateMutex then // create the handle, to determine whether this application runs begin Application.CreateForm (TfrmMain, frmMain) in; Application.CreateForm (Tform2, form2); Application.CreateForm (TDataModule1, DataModule1); Application.Run; end else begin DESTROYMUTEX; / / Release the handle end; end.

(2) Landing form umain.pas umain.dfm // unit umain;

Interfaceuses ... type .....PE ... ... ... private public end;

Var frmmain: tfrmmain;

Function Createmutex: boolean; // All project utility procedure destroymutex; // full project utility function

ImplementationUses UDataModule; // Reference Data Module Var Mutex: hwnd;

{$ R * .dfm}

Procedure Destroymutex; Begin IF Mutex <> 0 Then CloseHandle (MUTEX); END;

function CreateMutex: Boolean; var PrevInstHandle: THandle; AppTitle: PChar; begin AppTitle: = StrAlloc (100); StrPCopy (AppTitle, Application.Title); Result: = True; Mutex: = Windows.CreateMutex (nil, False, AppTitle) ; if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin Result: = False; SetWindowText (Application.Handle, ''); PrevInstHandle: = FindWindow (nil, AppTitle); if PrevInstHandle <> 0 then begin if IsIconic ( PrevInstHandle) then ShowWindow (PrevInstHandle, SW_RESTORE) else BringWindowToTop (PrevInstHandle); SetForegroundWindow (PrevInstHandle); end; if Mutex <> 0 then Mutex: = 0; end; StrDispose (AppTitle); end;

转载请注明原文地址:https://www.9cbs.com/read-109221.html

New Post(0)