Biraz Muharrem Bey'in kulaklarını çınlatalım; hani onun sürekli dediği gibi bakalım aşağıdaki sihri görebilecek misiniz
Kod: Tümünü seç
program pRestartApplication;
uses
WinAPI.Windows,
Vcl.Forms,
uRestartApplication in 'uRestartApplication.pas' {frmRetartApp};
{$R *.res}
function IsRestarted : Boolean;
begin
Result := false;
MutexHandle := OpenMutex(MUTEX_ALL_ACCESS, false, 'Restart_Mutex_By_Tugrul');
if MutexHandle <> 0 then
begin
WaitForSingleObject(MutexHandle, 1001);
Result := true;
end;
end;
begin
bIsRestarted := IsRestarted;
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TfrmRetartApp, frmRetartApp);
frmRetartApp.RestartStatus := bIsRestarted;
Application.Run;
if MutexHandle <> 0 then
begin
ReleaseMutex(MutexHandle);
CloseHandle(MutexHandle);
end;
end.
Kod: Tümünü seç
unit uRestartApplication;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TfrmRetartApp = class(TForm)
lblMessage: TLabel;
btnRestartApp: TButton;
procedure btnRestartAppClick(Sender: TObject);
private
{ Private declarations }
procedure SetRestartStatus(const Value : Boolean);
public
{ Public declarations }
property RestartStatus : Boolean write SetRestartStatus;
end;
var
frmRetartApp: TfrmRetartApp;
bIsRestarted : Boolean;
MutexHandle : NativeUInt;
implementation
{$R *.dfm}
procedure TfrmRetartApp.btnRestartAppClick(Sender: TObject);
var
PI : TProcessInformation;
SI : TStartupInfo;
FileName : PWideChar;
begin
GetStartupInfo(SI);
GetModuleFileName(0, FileName, MAX_PATH);
FillChar(PI, SizeOf(TProcessInformation), 0);
MutexHandle := CreateMutex(nil, true, 'Restart_Mutex_By_Tugrul');
if CreateProcess(FileName, GetCommandLine(), nil, nil, false, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
begin
Sleep(1000);
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
Close;
end;
end;
procedure TfrmRetartApp.SetRestartStatus(const Value: Boolean);
begin
if Value
then lblMessage.Caption := 'Restarted'
else lblMessage.Caption := 'Yeni başlamış uygulama';
end;
end.