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

關於執行緒與Timer的問題??

答題得分者是:william
BOOK
一般會員


發表:19
回覆:18
積分:7
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-03-12 19:52:56 IP:140.135.xxx.xxx 未訂閱
請問: 執行緒跟Timer的功能有什麼差別,哪個使用起來的效果比較好,如果我的程式是需要不斷地監控一些目錄,並把有變動的資料記錄起來,這樣的話用哪個會比較好,目錄的數目會隨著使用者而不斷變動(可能新增或減少),所以可能需要多緒或是多個Timer?? 謝謝各位!!
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-03-13 09:33:18 IP:147.8.xxx.xxx 未訂閱
Thread is able to run standalone while timer can be thought of some kind of interrupt which makes use of the Windows messages. A thread (except the main thread) do not have its own message dispatcher so you need to implement one within your thread if you want to use a timer (or other Windows messages handler) within a thread. BTW, timer DOES use system resource (not RAM available) and I think there is a limit of 32? in Windows 95 (I think others has a larger limit) while the max. number of threads is determinied by the system configuration and I remember I got a max. of ~120 threads before crashing a Windows 95 (1st version, 64M RAM).  >
BOOK
一般會員


發表:19
回覆:18
積分:7
註冊:2002-04-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-04-04 16:38:05 IP:140.135.xxx.xxx 未訂閱
請教幾各問題: 1.Timer可以不斷反覆的持續運作,而執行緒做完了就會停止住,要如何使執行緒跟Timer一樣能夠不斷的運作下去,舉例來說當開始監控目錄資料並記錄後,Timer會在interval的時間後又重複作此動作(因為不知何時檔案會異動所以只好不斷的監控他),而執行緒則必須再重新execute,是否需要利用Timer不斷去execute執行緒,執行緒才能反覆運作,如果需要這樣做的話,會不會有當Timer重複運作而執行緒還未停止的問題?? 2.如果必須還是利用Timer去不斷execute執行緒,那麼有無方法可以偵測執行緒是否已運作完成,避免同時execute相同功能之執行緒?? 3.最後,再問一各問題,像這樣監控目錄及備份異動過後的檔案(以一台電腦的資料為主,比對兩邊檔案的最後異動時間作為是否更新檔案之依據),是否有更好的寫法或是演算法?? 謝謝各位前輩!!
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-04-04 17:12:58 IP:147.8.xxx.xxx 未訂閱
You should prevent the thread from ending itself... e.g.
procedure TMyThread.Execute;
begin
    while not Terminated do begin
        {....}
        sleep(0); // release CPU! for other process/thread
    end;
end;
Backup of network files is trickly since the system time of different machines could be different. If you are using Windows 2000 Servers, you may be interested in domain DFS, which should be a fault tolerant one. http://www.microsoft.com/windows2000/techinfo/planning/fileandprint/dfssteps.asp
BOOK
一般會員


發表:19
回覆:18
積分:7
註冊:2002-04-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-04-05 00:32:24 IP:140.135.xxx.xxx 未訂閱
再請問幾個問題: 1.這樣可以做到類似Timer持續運作的功能,那請問當我想停止該執行緒的話,該如何作才可以?? 2.關於資料備份的問題,我的環境是在主-備兩台電腦的情況下,當主電腦檔案異動時,便透過此執行緒將異動檔案備份過去,目前是利用網路磁碟機的方式監控主電腦之相關目錄(由於目錄裡可能還有子目錄,因此採用遞回的方式來檢測檔案最後異動時間),然後再下達win32api的copyfile,不知各位前輩有無更好的方法可以增進備份的效率,或是我這樣的做法有不好的地方?? (PS.非常謝謝william前輩不厭其煩的指教)
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-04-06 13:19:52 IP:210.3.xxx.xxx 未訂閱
1. Thread.Terminate 2. I think you cannot copy opened files (those without proper file sharing enabled) this way... this is why those backup utilities (which claim to be able to backup opened files) are so expensive  href="http://hem.spray.se/anders.peterson/nhb.html">http://hem.spray.se/anders.peterson/nhb.html
BOOK
一般會員


發表:19
回覆:18
積分:7
註冊:2002-04-15

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-04-15 00:01:40 IP:163.28.xxx.xxx 未訂閱
請問: 1.我使用Thread"不斷"去監控目錄異動資料,會造成CPU的Usage持續維持在100%,使用Timer也是會造成相同結果,要如何解決如此的問題(PS.我想其他的應用程式都多多少少會用到Thread,為何它們卻不會大量佔用CPU的Resource)?? 2.使用Terminate會在跑完最後一次流程後才會結束,是否有即時中斷之方法(要能夠適時的釋放資源)?? 非常謝謝各位!!
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-04-15 09:13:35 IP:147.8.xxx.xxx 未訂閱
1. Try adding a sleep(x) inside your loop to release the CPU. 2. You can use the API TerminateThread to kill the thread but it won't allow the thread to relase any resource. I think you should check the thread's Terminated property within your loop and quit as needed.
BOOK
一般會員


發表:19
回覆:18
積分:7
註冊:2002-04-15

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-04-15 15:14:05 IP:163.28.xxx.xxx 未訂閱
請問: 1.我用Sleep(0)時(之前已經有加在程式內),CPU的Usage一樣是100%,如果改成Sleep(1000),它的Usage就會在40%-70%之間,如果改成Sleep(10000),則大約都是在10%以下,請問在執行緒內用Sleep跟一般在應用程式內用Sleep是不是不一樣,我測試的結果是在執行緒內使用時其他程式會有反應(拉一各按鍵裡面寫ShowMessage),但若不是在執行緒內則會等他Sleep時間到以後才ShowMessage,這兩者差異在哪裡(聽說Sleep會讓電腦真的ZZZZ.....那會不會有負面效果)?? 2.關於我的程式還是希望能達到"即時不斷"監控原則的話,如果不用Sleep的話,還有無其他方式release CPU的Resource,雖說Sleep越久資源就佔的越少,但相對的就比較無法達到即時的目的,是否有兩者兼顧的辦法?? 謝謝!!
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-04-15 15:50:39 IP:147.8.xxx.xxx 未訂閱
1. No harm in using sleep. It acts like suspending the thread for a short period and wake it up later. I think your polling of the directory is too frequent? 2. You may want to take a look at FindFirstChangeNotification. BTW, RxLib has a component for monitoring folders.
BOOK
一般會員


發表:19
回覆:18
積分:7
註冊:2002-04-15

發送簡訊給我
#11 引用回覆 回覆 發表時間:2003-04-23 00:05:59 IP:140.135.xxx.xxx 未訂閱
請問 : 1.我用FindFirstChangeNotification去監控資料夾的異動,可是好像不能知道異動的檔案名稱,只能知道此資料夾異動過,不知道有什麼辦法可以知道異動過的"檔案名稱"?? 2.我查過相關資料好像ReadDirectoryChangesW這個函數可以傳回異動的檔案名稱,可是看不太懂Help裡的用法說明,可否請教各位這個函數應該怎麼應用呢?? 謝謝各位!!
系統時間:2024-05-16 21:12:29
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!