如何判斷及關閉正在執行中的程式 |
答題得分者是:pceyes
|
rany
一般會員 發表:15 回覆:26 積分:8 註冊:2007-05-05 發送簡訊給我 |
請問各位大大,
我用了一個MENU當目錄去呼叫別隻執行檔,但是為了資料庫資源考量,不希望USER重複開啟同樣的程式。再加上因為每次呼叫時MENU都會傳參數給被呼叫的執行檔的考量,所以不知道DELPHI可不可以做到,當重複執行時先關閉原本的執行檔,然後再重新開啟呢? 目前只知道可以知道被呼叫的程式是否執行中,如下: IF FINDWINDOW('TAPPLICATION',pchar('EXE.exe')) <> 0 -->正被執行中 請提供FINDWINDOW這個函含數更多的應用或或相關參考的程式碼,QQQ.... (環境: D7 / Oracle / W-XP) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
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元件可以直接使用。
------
努力會更接近成功 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
真的找到妳要的資料,否則又被說所答非所問,
主要是關閉另一個程式(不過我還是要雞婆:誏他自己走,比你請他走還要好,一如人生一樣) 裏面的例子正好是用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 發送簡訊給我 |
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 發送簡訊給我 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rany
一般會員 發表:15 回覆:26 積分:8 註冊:2007-05-05 發送簡訊給我 |
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, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |