關於如何避免程式重複執行 |
答題得分者是:andersonhsieh
|
JalenKu
一般會員 發表:14 回覆:24 積分:7 註冊:2002-06-10 發送簡訊給我 |
站長你好:
關於程式避免重複執行的問題,在發問之前我參考過bruce0211的BCB
及站長所寫的範例了(我有先用尋找文章喔),不過實在是很不好意思,因為對BCB實在不懂,所以只好放棄了
而站長所寫的範例程式
function IsExecute(filename:string):boolean; var MyWnd : Thandle; begin MyWnd:=FindWindow(nil, pchar(filename)); result:=MyWnd<>0; end; procedure TForm1.Button1Click(Sender: TObject); begin if IsExecute('project1') then showmessage('Project1 is running'); end;我將其寫在FormCreate中,可是如何讓其第一次執行時,不會顯示running 而重複執行時才會顯示error,並中止呢.. 也就是只要不重複執行就ok.. if you lose your step,just tango on
------
if you lose your step,just tango on |
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
引言: 站長你好: 關於程式避免重複執行的問題,在發問之前我參考過bruce0211的BCB 及站長所寫的範例了(我有先用尋找文章喔),不過實在是很不好意思,因為對BCB實在不懂,所以只好放棄了 而站長所寫的範例程式可以試試application.terminate; 程式碼如下: function IsExecute(filename:string):boolean; var MyWnd : Thandle; begin MyWnd:=FindWindow(nil, pchar(filename)); result:=MyWnd<>0; end; procedure TForm1.FormCreate(Sender: TObject); begin if IsExecute('project1') then application.terminate end; ~~~Delphi K.Top討論區站長~~~function IsExecute(filename:string):boolean; var MyWnd : Thandle; begin MyWnd:=FindWindow(nil, pchar(filename)); result:=MyWnd<>0; end; procedure TForm1.Button1Click(Sender: TObject); begin if IsExecute('project1') then showmessage('Project1 is running'); end;我將其寫在FormCreate中,可是如何讓其第一次執行時,不會顯示running 而重複執行時才會顯示error,並中止呢.. 也就是只要不重複執行就ok.. if you lose your step,just tango on
------
~~~Delphi K.Top討論區站長~~~ |
JalenKu
一般會員 發表:14 回覆:24 積分:7 註冊:2002-06-10 發送簡訊給我 |
天使大大:
謝謝你的回覆,不過,如果照站長所寫的程式
procedure TForm1.FormCreate(Sender: TObject); begin if IsExecute('project1') then application.terminate end;那麼我連第一隻程式都無法run,因為只要每次在執行此程式 使就中斷了,是否可以判斷該程式只能執行一次,就是有一個 count的方式之類,不好意思...再麻煩你了.. if you lose your step,just tango on
------
if you lose your step,just tango on |
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
引言: 天使大大: 謝謝你的回覆,不過,如果照站長所寫的程式將程式碼寫在.DPR中 program Project1; uses Forms, Unit1 in 'Unit1.pas' {Form1}; {$R *.RES} begin Application.Initialize; if IsExecute('project1') then application.terminate else begin Application.CreateForm(TForm1, Form1); Application.Run; end; end. ~~~Delphi K.Top討論區站長~~~procedure TForm1.FormCreate(Sender: TObject); begin if IsExecute('project1') then application.terminate end;那麼我連第一隻程式都無法run,因為只要每次在執行此程式 使就中斷了,是否可以判斷該程式只能執行一次,就是有一個 count的方式之類,不好意思...再麻煩你了.. if you lose your step,just tango on
------
~~~Delphi K.Top討論區站長~~~ |
JalenKu
一般會員 發表:14 回覆:24 積分:7 註冊:2002-06-10 發送簡訊給我 |
program Project1; uses Forms,windows, Unit1 in 'Unit1.pas' {Form1}; {$R *.RES} var win:thandle; begin Application.Initialize; win:=FindWindow(nil, pchar('project1')); if win<>0 then application.terminate else begin Application.CreateForm(TForm1, Form1); Application.Run; end; end.站長大大,還是不行,我每次跑都會跑到 appication.terminate 就停了... if you lose your step,just tango on
------
if you lose your step,just tango on |
andersonhsieh
版主 發表:33 回覆:531 積分:439 註冊:2002-06-10 發送簡訊給我 |
|
JalenKu
一般會員 發表:14 回覆:24 積分:7 註冊:2002-06-10 發送簡訊給我 |
|
andersonhsieh
版主 發表:33 回覆:531 積分:439 註冊:2002-06-10 發送簡訊給我 |
我找到一個物件給你參考
unit PBJustOne; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TPBJustOne = class(TComponent) private protected public constructor Create(AOwner: TComponent); override; published end; procedure Register; implementation const AllowedInstances = 1; var MyAppName, MyClassName: array[0..255] of Char; NumFound: Integer; LastFound, MyPopup: HWND; function LookAtAllWindows(Handle: HWND; Temp: LongInt): BOOL; stdcall; var WindowName, ClassName: Array[0..255] of Char; begin // Go get the windows class name if GetClassName(Handle, ClassName, SizeOf(ClassName)) > 0 then // Is the window class the same ? if StrComp(ClassName, MyClassName) = 0 then // Get its window caption if GetWindowText(Handle, WindowName, SizeOf(WindowName)) > 0 then // Does this have the same window title ? if StrComp(WindowName, MyAppName) = 0 then begin inc(NumFound); // Are the handles different ? if Handle <> Application.Handle then // Save it so we can bring it to the top later. LastFound := Handle; end; result := true; end; procedure Register; begin RegisterComponents('System', [TPBJustOne]); end; constructor TPBJustOne.Create(AOwner: TComponent); begin inherited; NumFound := 0; LastFound := 0; // First, determine what this application'name is. GetWindowText(Application.Handle, MyAppName, SizeOf(MyAppName)); // Now determine the class name for this application GetClassName(Application.Handle, MyClassName, SizeOf(MyClassName)); // Now count how many others out there are Delphi apps with this title EnumWindows(@LookAtAllWindows, 0); if NumFound > AllowedInstances then // there is another instance running, bring it to the front ! begin MyPopup := GetLastActivePopup(LastFound); // Bring it to the top in the Z-Order BringWindowToTop(LastFound); // Is the window iconized ? if IsIconic(MyPopup) then // Restore it to its original position ShowWindow(MyPopup, SW_RESTORE) else // Bring it to the front SetForegroundWindow(MyPopup); // Halt this instance Halt; end end;@@~~飛翔在天際的精靈~~@@
------
@@~~飛翔在天際的精靈~~@@ |
cccheng
一般會員 發表:8 回覆:19 積分:5 註冊:2002-05-30 發送簡訊給我 |
在 Project1 中加入...可按Ctrl-F12 選出 program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
const <==== 加入箭頭所指
classname = 'TForm1'; <==== 預設為 TForm1
var <====
handle : Integer; <====
begin
handle := findwindow(classname, nil); <====
if handle <> 0 then halt; <==== 重複執行 離開 Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
|
JalenKu
一般會員 發表:14 回覆:24 積分:7 註冊:2002-06-10 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |