PDA

View Full Version : جلوگیری از اجرای همزمان یک برنامه



SReza1
چهارشنبه 19 شهریور 1382, 11:36 صبح
سلام
میخواستم بدونم چطور میشه از اجرای یک برنامه چند بار روی یک کامپیوتر جلوگیری کرد؟
احتمالا باید از خواندن و نوشتن در حافظه استفاده کرد! منو راهنمایی کنید

مهدی کرامتی
چهارشنبه 19 شهریور 1382, 12:04 عصر
خوب معلومه که باید از نوشتن در حافظه استفاده کرد!!!
برای انجام اینکار از Mutex ها استفاده میکنیم:

کد برای استفاده در سورس پروژه:
{The code for ONEINSTANCE.dpr}

program ONEINSTANCE;

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

{$R *.RES}

begin
{Attempt to create a named mutex}
CreateMutex(nil, false, 'MyApp');
{if it failed then there is another INSTANCE}
if GetLastError = ERROR_ALREADY_EXISTS then begin
{Send all windows our custom message - only our other}
{INSTANCE will recognise it, and restore itself}
SendMessage(HWND_BROADCAST,
RegisterWindowMessage('MyApp'),
0,
0);
{Lets quit}
Halt(0);
end;
Application.Initialize;
{Tell Delphi to un-hide it's hidden application window}
{This allows our INSTANCE to have a icon on the task bar}
Application.ShowMainForm := true;
ShowWindow(Application.Handle, SW_RESTORE);
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

کد برای استفاده در Unit1.pas:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
OldWindowProc : Pointer; {Variable for the old windows proc}
MyMsg : DWord; {custom systemwide message}

function NewWindowProc(WindowHandle : hWnd;
TheMessage : LongInt;
ParamW : LongInt;
ParamL : LongInt) : LongInt stdcall;
begin
if TheMessage = MyMsg then begin
{Tell the application to restore, let it restore the form}
SendMessage(Application.handle, WM_SYSCOMMAND, SC_RESTORE, 0);
SetForegroundWindow(Application.Handle);
{We handled the message - we are dONE}
Result := 0;
exit;
end;
{Call the original winproc}
Result := CallWindowProc(OldWindowProc,
WindowHandle,
TheMessage,
ParamW,
ParamL);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
{Register a custom windows message}
MyMsg := RegisterWindowMessage('MyApp');
{Set form1's windows proc to ours and remember the old window proc}
OldWindowProc := Pointer(SetWindowLong(Form1.Handle,
GWL_WNDPROC,
LongInt(@NewWindowProc)));
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{Set form1's window proc back to it's original procedure}
SetWindowLong(Form1.Handle,
GWL_WNDPROC,
LongInt(OldWindowProc));
end;

begin
{Tell Delphi to hide it's hidden application window for now to avoid}
{a "flash" on the taskbar if we halt due to another INSTANCE}
ShowWindow(Application.Handle, SW_HIDE);
end.

artemis-a..x
چهارشنبه 19 شهریور 1382, 13:54 عصر
اسیستنت جان

آقا شما که یه برنامه میذاری اینجا ... عزیزم یک فایل مثال هم اینجا بگذار.
بابا N تا پی ام داشتم که هی میگفتن برنامه درست کار نمیده و اگر یک فایل مثال داری .. بگذار.

قربانت.
آرتمیس.

SReza1
پنج شنبه 20 شهریور 1382, 15:32 عصر
سلام
با کمک شما مشکل من حل شد. ممنون از کمکتون

Gladiator
شنبه 17 آبان 1382, 22:12 عصر
با سلام

حالا اگر بخوایم برنامه ای با 2 فایل اجرایی داشته باشیم چی ؟

مثلا :
یه برنامه داریم که قسمتی برای انجام تنظیمات داره . این برنامه خارج از برنامه اصلی قرار میگیره . حالا میخوای هر وقت برنامه اصلی در حال اجراست این برنامه تنظیمات اجرا نشه .

با تشکر .

phantasm
یک شنبه 18 آبان 1382, 05:57 صبح
سلام
برای این کار فکرکنم شما باید در برنامه ی دوم( برنامه تنظیمات )mutex برنامه اول را چک کنید .یعنی در برنامه اصلی این کد رو بزارید:


program Project1;

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

{$R *.res}
var
hMutex: THandle;


begin
hMutex := OpenMutex(MUTEX_ALL_ACCESS, false, 'myiqueName');

if hMutex <> 0 then begin
CloseHandle(hMutex);
exit;
end;

CreateMutex(nil, true, 'myiqueName');


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

و در برنامه تنظیمات فقط لازمه mutex برنامه اول چک بشه . اینجوری:


program Project2;

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

{$R *.res}
var
hMutex: THandle;


begin
hMutex := OpenMutex(MUTEX_ALL_ACCESS, false, 'myiqueName');

if hMutex <> 0 then begin
CloseHandle(hMutex);
exit;
end;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;

end.

Gladiator
یک شنبه 18 آبان 1382, 08:05 صبح
دوست عزیز با تشکر از لطف شما . حتما امتحانش میکنم .

متشکرم .

phantasm
یک شنبه 18 آبان 1382, 08:57 صبح
خواهش میکنم عزیزم قابلی نداشت :wink: