關於執行緒與Timer的問題?? |
答題得分者是:william
|
BOOK
一般會員 發表:19 回覆:18 積分:7 註冊:2002-04-15 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
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 發送簡訊給我 |
請教幾各問題: 1.Timer可以不斷反覆的持續運作,而執行緒做完了就會停止住,要如何使執行緒跟Timer一樣能夠不斷的運作下去,舉例來說當開始監控目錄資料並記錄後,Timer會在interval的時間後又重複作此動作(因為不知何時檔案會異動所以只好不斷的監控他),而執行緒則必須再重新execute,是否需要利用Timer不斷去execute執行緒,執行緒才能反覆運作,如果需要這樣做的話,會不會有當Timer重複運作而執行緒還未停止的問題?? 2.如果必須還是利用Timer去不斷execute執行緒,那麼有無方法可以偵測執行緒是否已運作完成,避免同時execute相同功能之執行緒?? 3.最後,再問一各問題,像這樣監控目錄及備份異動過後的檔案(以一台電腦的資料為主,比對兩邊檔案的最後異動時間作為是否更新檔案之依據),是否有更好的寫法或是演算法?? 謝謝各位前輩!!
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
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 發送簡訊給我 |
再請問幾個問題: 1.這樣可以做到類似Timer持續運作的功能,那請問當我想停止該執行緒的話,該如何作才可以?? 2.關於資料備份的問題,我的環境是在主-備兩台電腦的情況下,當主電腦檔案異動時,便透過此執行緒將異動檔案備份過去,目前是利用網路磁碟機的方式監控主電腦之相關目錄(由於目錄裡可能還有子目錄,因此採用遞回的方式來檢測檔案最後異動時間),然後再下達win32api的copyfile,不知各位前輩有無更好的方法可以增進備份的效率,或是我這樣的做法有不好的地方?? (PS.非常謝謝william前輩不厭其煩的指教)
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
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 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
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 發送簡訊給我 |
請問: 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 發送簡訊給我 |
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 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |