Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

TheMaker

Fazer q o programa execute só uma vez

Recommended Posts

Achei esse código na Web... Insira no arquivo .DPR

 

{$R *.res}beginApplication.Initialize;////////////////////////////////////////////////CreateMutex(nil, True, 'CodigoParaExcucaoUnicaDoAplicativo');if GetLastError = ERROR_ALREADY_EXISTS thenbegin   messagedlg('O sistema já foi inicializado!',mtinformation,[mbok],0);   exit;end;////////////////////////////////////////////////Application.Title := 'Fidelizando';Application.CreateForm(TFLogin, FLogin);Application.CreateForm(TFPrincipal, FPrincipal);....

Compartilhar este post


Link para o post
Compartilhar em outros sites

Hummm.. é bem mais complicado pois é necessário "achar" a janela do programa anterior...

 

Achei o código abaixo no CODCENTRAL da Borland. Esta bem documentado... mas mesmo assim é bem complexo...

 

(* Just include this unit in your project *)(* ONE Instance APP   Author  : Clément Doss   Date    : 6 - DEZ - 2000   VErsion : 0.0 - First relaease*)(* If you have any suggestions or bug reports please do   email it to suporte@dhs.com.br *)(* How it works *)(* I am just trying to keep it simply stupid. I just wanted to add a unit   to my project that will ensure that one and only one instance will be   running.   I never liked to change the Project Source to add stuff there... That´s why   I wrote this unit.   Just Add this unit to your project and only one application (with the same   title) will be allowed to run.*)(* A few more details....   I am using the Application.Title property as an application identifier.   If you are using two application with the same Application.Title, then the   second one will not open....   I used the Title because it seems to be adequate to the following rules:      1) No code will be added. Just add the unit!      2) Two applications with the same title seems unlikely to happen.      3) ... Just not figured this one yet...   I am using MUTEXes to check the existance of a second instance of the same   application.   Both CreateMutex and WaitForSingleObjects are available for Win9x and Windows NT.   I have tested it with Windows 2000 pro, and delphi 5 E. But it should work with   earlier versions of delphi.   Enjoy... *)unit uDHSOneInstanceApp;interfaceimplementationuses Windows,   // THandle defined here     SysUtils,  // strPCopy defined here     Forms;     // Application defined heretype  TdhsOneInstanceApp = Class(TObject)  private    FMutex     : THandle;    FAppTitle  : Array[0..$100] of char;    procedure CheckForAnotherInstance;  public    { Public declarations }    constructor Create;  end;(* //////////  \\\\\\\\\\ *)var   OneInstanceApp : TdhsOneInstanceApp;{ TdhsOnceInstance }constructor TdhsOneInstanceApp.Create;begin     strPCopy(FappTitle,Application.Title);     CheckForAnotherInstance;end;procedure TdhsOneInstanceApp.CheckForAnotherInstance;var   hRunningApp : THandle;begin  // .. This mutex will be freed when the applcation closes!  FMutex := CreateMutex (nil, FALSE, FAppTitle );  if WaitForSingleObject (FMutex, 0) = wait_TimeOut then begin     // Found another instance     SetWindowText(Application.Handle,''); // We must not found this one again;)     // Retrieving handle of the first instance;     hRunningApp := FindWindow(nil,FAppTitle);     if hRunningApp<>0 then begin        if IsIconic(hRunningApp) then           // First instance is minimized.. Restore it           ShowWindow(hRunningApp,SW_RESTORE);        BringWindowToTop(hRunningApp);        SetForegroundWindow(hRunningApp);     end;     // Finish this second instance ...     Application.ShowMainForm := False; // Well... don´t show the main windows     Application.Terminate;     Halt(2);  end;end;initialization   OneInstanceApp := TdhsOneInstanceApp.Create;finalization   OneInstanceApp.Free;end.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.