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

如何判斷及關閉正在執行中的程式

答題得分者是:pceyes
rany
一般會員


發表:15
回覆:26
積分:8
註冊:2007-05-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-07-03 14:21:55 IP:220.132.xxx.xxx 訂閱
請問各位大大,

我用了一個MENU當目錄去呼叫別隻執行檔,但是為了資料庫資源考量,不希望USER重複開啟同樣的程式。再加上因為每次呼叫時MENU都會傳參數給被呼叫的執行檔的考量,所以不知道DELPHI可不可以做到,當重複執行時先關閉原本的執行檔,然後再重新開啟呢?

目前只知道可以知道被呼叫的程式是否執行中,如下:
IF FINDWINDOW('TAPPLICATION',pchar('EXE.exe')) <> 0 -->正被執行中

請提供FINDWINDOW這個函含數更多的應用或或相關參考的程式碼,QQQ....
(環境: D7 / Oracle / W-XP)
編輯記錄
rany 重新編輯於 2008-07-03 14:23:13, 註解 無‧
rany 重新編輯於 2008-07-03 14:25:05, 註解 無‧
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-07-03 16:24:01 IP:220.141.xxx.xxx 訂閱
Win32 API 使用討論區(Delphi) » 如何使用mutex判斷程式是否已經被執行?
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=92476

如何用 CreateMutex 避免程式重複執行 ?
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=85340
Win32 API 使用討論區(Delphi) » 使用OpenMutex方法,去防止程式重複執行的問題
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=68767
用程序 (process) 數量判斷是否重複執行函式
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=86113
Win32 API 使用討論區(Delphi) » 第二次運行時自動關閉程式如何實現?
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=86099
........
findwindow ,findwindowex 據傳不是很準,
see this
Win32 API 使用討論區(Delphi) » FindWindow
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=73523

所以多用CreateMute, Thread,ATOM,mappingfile等方式來完成,如果時間不允許
網上也有很多VCL元件可以直接使用。

Deriver v.1.0FWS15 k24 Feb 1997
By Mauro Venturini. Polymorphic Creation of Instances, that is a way to create an instance whose class type depends on run time parameters. Fully functional Source: Included
Download: D1 D2

Fnugry CloneControl v.1.0.1.2FWS71 k18 Aug 1997
By Gleb Yourchenko. Fnugry CloneControl Component is a simple tool that allows developer to control a number of application instances user can run, and define an action to be taken when a maximum number of running instances achieved. Fully functional Source: Included
Download: D3

JustOne v.1.3FWS37 k26 Mar 1997
By Steve Keyser. JustOne is a component which makes it easy to limit the number of your application's instances to just one. If a second instance of your application starts, the first instance is brought to the front and given the focus (or restored if it was minimized to an icon). The second instance then halts. Fully functional Source: Included
Download: D1 D2

OneInstance v.2.3FWS6 k23 Sep 2002
By Andrew Johnson. To allow only a single instance of an application - can warn user, terminate new instance, use custom handler, and/or Raise Former Instance of Application when another instance is found. Simply drop one on your main form. Fully functional Source: Included
Download: D4 D5 D6 D7 Homepage

PrevInst v.1.0FWS1 k17 Jan 1997
By Bill Florac. Unit to determine if Application is already running. Works with Delphi 2 and Win95 only. Done at application level so form is never shown. Fully functional Source: Included
Download: D2

TApp Instance v.1.0FWS6 k24 Oct 1997
By Thomas Schubbauer. TAppInstance controls all instances of a program in a network. It can be used, to limit the count of users which can run the program at the same time. Fully functional Source: Included
Download: D2 D3 Homepage

TFnugry SingleInstance v.1.00FWS66 k02 Oct 1997
By Gleb Yourchenko. TFnugrySingleInstance is a small component that prevents user from running multiple instances of application. The usage is very simple: just drop the component to application's main form. By default, when TFnugrySingleInstance detects a previous instance running, it brings previous instance's main form to front and closes application using Application.Terminate method. You can customize component's behavior by writing OnAlreadyRunning event handler. Fully functional Source: Included Exe-Demo Included
Download: D3

TMain Instance v.1.0FWS5 k31 Mar 1998
By Pal Sitkei. The TMainInstance component allows only one instance of the current exe. The started second instance send a message to the first with the given parameter and closes self. The main instance recives new running parameter. Fully functional Source: Included
Download: D3 Homepage

TMultiInstance v.1.0FWS4 k17 May 2002
By 2p plus. TMultiInstance component controls behavior of multiple intances of the same program. By default allows only one instance of program. Next instance terminates immediatly. Fully functional Source: Included
Download: D5 D6 Homepage

TOneInstance v.1.01FWS6 k09 Sep 2006
By Florian Bomers. Prevents that your application may be started more than once. To the first instance are passed the parameters with which the second instance was started. Demonstrates use of memory mapped files and semaphores. Fully functional Source: Included
Download: D2 D3 Homepage

TOnlyOpenOnce v.2.0FWS4 k03 Jul 1997
By Sean Adams. Drop this on the main form of your app, and only one copy will be able to be loaded. Win32 safe, and it uses fileMappings so it can be used in a truly multitasking environment. Fully functional Source: Included
Download: D2

TPBJustOne v.1.05FWS4 k29 May 2007
By Patrick Brisacier, Devonic, Inc. A component that enables only one unique instance of an application at each time. Fully functional Source: Included
Download: D2 D2005 D2006 D2007 D3 D4 D5 D6 D7 Homepage

TShellWIndow Observer v.1.2FWS50 k10 Sep 2001
By Diego Amicabile. "This component was born out of frustration, since I wasn't able to connect to an existing instance of Internet Explorer and catch the events it fires. Using this component you can, and you can perform actions when a new instance is started or an existing instance moves to a new location." Added the improvements from Michel Hibon and the demo from Randy Williams. Fully functional Source: Included
Download: D3 D4 D5 D6 Homepage

TmgPrevInstance v.1.1.0FWS7 k27 Mar 1997
By Malcolm Groves. TmgPrevInstance is a simple component that tests for any other instances of the current EXE running. You can specify how many instances are allowed to run at any one time, the action to take when an instance exceeding the specified number is found and specifiy your own code in an event that fires when an instance exceeding the specified number is found. Fully functional Source: Included
Download: D1 D2 Homepage

XJustOne v.2.5FWS1 k11 Nov 1999
By Gennady Makeev, UtilMind Solutions. Two components for starting only one copy of application:
  • TJustOne - immediatelly close second copy;
  • TJustOneAsk - asks user about second copy and close it in case of negative answer.
Fully functional Source: Included
Download: CB1 CB3 CB4 D2 D3 D4 Homepage

------
努力會更接近成功
編輯記錄
pceyes 重新編輯於 2008-07-03 16:26:51, 註解 無‧
pceyes 重新編輯於 2008-07-03 16:26:58, 註解 無‧
pceyes 重新編輯於 2008-07-04 11:31:33, 註解 無‧
pceyes 重新編輯於 2008-07-04 11:59:25, 註解 無‧
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-07-04 12:12:40 IP:122.118.xxx.xxx 訂閱
真的找到妳要的資料,否則又被說所答非所問,
主要是關閉另一個程式(不過我還是要雞婆:誏他自己走,比你請他走還要好,一如人生一樣)
裏面的例子正好是用Findwindows
:

...kill a task?
Author: Misha Moellner
Homepage: http://www.delphi3000.com
取自
http://www.swissdelphicenter.ch/torry/showcode.php?id=266
For Windows 9x/ME/2000/XP }
uses
Tlhelp32;
function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
KillTask('notepad.exe');
end;
{ For Windows NT/2000/XP }
procedure KillProcess(hWindowHandle: HWND);
var
hprocessID: INTEGER;
processHandle: THandle;
DWResult: DWORD;
begin
SendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0,
SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult);
if isWindow(hWindowHandle) then
begin

//FindWindow('notepad',nil));
end;
------
努力會更接近成功
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-07-04 17:12:01 IP:220.141.xxx.xxx 訂閱
 bruce0211---BCB專欄 » 心得分享系列(BCB)
http://delphi.ktop.com.tw/board.php?cid=169&fid=936&tid=18760

心得分享 : 防止程式重複執行

>>要防止自己的程式重複執行,使用的方法有好幾種,有些方法甚至要去修改*.dpr檔(Delphi 專案檔),但>>bruce0211不建議讀者任意修改 Delphi自動維護的專案檔;使用本節介紹的方法只要將下列程式碼原封不>>動的抄到您的 FormCreate() 事件中即可,不但不會讓相同的程式同時存在第二個實體,而且在執行時發>>現第一個實體已存在而且是最小化時,還會將其彈出展開於桌面上....


我將它由BCB翻譯成Delphi

procedure TForm1.FormCreate(Sender: TObject);
var
Application.Title := Self.Caption;
Mutex := OpenMutex(SYNCHRONIZE, False, Pchar(Application.Title));
If (Mutex <> 0) Then
Begin
AppTitle := Application.Title;
SetWindowText(Application.Handle, Nil);
PrevInstHandle := FindWindow('TApplication', Pchar(AppTitle));
if (PrevInstHandle <> 0) then
begin
// 前一程式 若最小化 回復原狀
if (IsIconic(PrevInstHandle)) then
ShowWindow(PrevInstHandle, SW_RESTORE);
// 將前一程式 設為 前景顯示
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
// 後來的同一程式 關閉
Application.ShowMainForm := False;
Application.Terminate;
end;
end
else
CreateMutex(Nil, False, Pchar(Application.Title));

// 接下來可以插入您其它要寫在 FormCreate 中的程式

end;
------
努力會更接近成功
rany
一般會員


發表:15
回覆:26
積分:8
註冊:2007-05-05

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-07-07 11:31:12 IP:220.132.xxx.xxx 訂閱
Dear pceyes,

謝謝你還幫我翻譯成Delphi語法呦!!^^"....
rany
一般會員


發表:15
回覆:26
積分:8
註冊:2007-05-05

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-07-07 11:47:11 IP:220.132.xxx.xxx 訂閱
Dear Pceyes,

別這麼說呀~
雖然我提問中提及Findwindows的用法
但是您的熱心已經讓我深感窩心,再怎樣都是另一個思考方向
不過我有測試了另一個Findwindows的寫法,程式碼CODE很少喔,不過比較不好的是得修改*.dpr檔(Delphi 專案檔),但是卻可以達到我A.EXE呼叫程式時會順便傳參數給B.EXE的功能,也順便提供給其他人參考唄~

[code delphi]

program Report;

uses
Forms, Windows, Messages, //STEP1
U_Form1 in 'U_Form1.pas' {Form1},
U_qrReport in 'U_qrReport.pas' {qrReport: TQuickRep},
U_qrReportA5 in 'U_qrReportA5.pas' {qrReportA5: TQuickRep},
U_FComment in 'U_FComment.pas' {F_Comment};
{$R *.res}

var hWnd: Integer; //STEP2
begin
//STEP3
hWnd:=FindWindow('TApplication',PChar('Report.exe'));
if hWnd<>0 then
begin
PostMessage(hWnd,WM_CLOSE,0,0);
end;

Application.Initialize;
Application.Title:='Report.exe'; //STEP4
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
[/code]


===================引 用 pceyes 文 章===================
真的找到妳要的資料,否則又被說所答非所問,
主要是關閉另一個程式(不過我還是要雞婆:誏他自己走,比你請他走還要好,一如人生一樣)
裏面的例子正好是用Findwindows

編輯記錄
rany 重新編輯於 2008-07-07 11:52:40, 註解 無‧
系統時間:2024-07-25 1:33:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!