全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2050
推到 Plurk!
推到 Facebook!

關於如何避免程式重複執行

答題得分者是:andersonhsieh
JalenKu
一般會員


發表:14
回覆:24
積分:7
註冊:2002-06-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-08-26 15:18:59 IP:203.66.xxx.xxx 未訂閱
站長你好: 關於程式避免重複執行的問題,在發問之前我參考過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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-08-26 17:29:11 IP:61.221.xxx.xxx 未訂閱
引言: 站長你好: 關於程式避免重複執行的問題,在發問之前我參考過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
可以試試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討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
JalenKu
一般會員


發表:14
回覆:24
積分:7
註冊:2002-06-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-08-27 09:23:00 IP:203.66.xxx.xxx 未訂閱
天使大大: 謝謝你的回覆,不過,如果照站長所寫的程式
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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-08-27 10:29:46 IP:61.221.xxx.xxx 未訂閱
引言: 天使大大: 謝謝你的回覆,不過,如果照站長所寫的程式
procedure TForm1.FormCreate(Sender: TObject);
begin
if IsExecute('project1') then application.terminate
end;
那麼我連第一隻程式都無法run,因為只要每次在執行此程式 使就中斷了,是否可以判斷該程式只能執行一次,就是有一個 count的方式之類,不好意思...再麻煩你了.. if you lose your step,just tango on
將程式碼寫在.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討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
JalenKu
一般會員


發表:14
回覆:24
積分:7
註冊:2002-06-10

發送簡訊給我
#5 引用回覆 回覆 發表時間:2002-08-27 15:15:27 IP:203.66.xxx.xxx 未訂閱
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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2002-08-27 15:34:42 IP:211.20.xxx.xxx 未訂閱
那是因為你在delphi中run的原因所造成的,將其build以後直接執行執行檔即可 @@~~飛翔在天際的精靈~~@@
------
@@~~飛翔在天際的精靈~~@@
JalenKu
一般會員


發表:14
回覆:24
積分:7
註冊:2002-06-10

發送簡訊給我
#7 引用回覆 回覆 發表時間:2002-08-28 08:23:12 IP:203.66.xxx.xxx 未訂閱
andersonhsieh大大: 我也試過了,仍然不行, 是否windows在create form之前 已將此執行緒載入,故我以findwindow去 尋找,總是terminate. if you lose your step,just tango on
------
if you lose your step,just tango on
andersonhsieh
版主


發表:33
回覆:531
積分:439
註冊:2002-06-10

發送簡訊給我
#8 引用回覆 回覆 發表時間:2002-08-28 17:14:17 IP:211.20.xxx.xxx 未訂閱
我找到一個物件給你參考  
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

發送簡訊給我
#9 引用回覆 回覆 發表時間:2002-08-28 17:54:56 IP:211.74.xxx.xxx 未訂閱
在 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

發送簡訊給我
#10 引用回覆 回覆 發表時間:2002-08-30 10:10:41 IP:203.66.xxx.xxx 未訂閱
謝謝andersonhsieh 和 cccheng的回應 andersonhsieh的物件方法較為複雜,故我採用cccheng的方式 謝謝兩位的不吝賜教 if you lose your step,just tango on
------
if you lose your step,just tango on
系統時間:2024-06-02 0:09:14
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!