SPComm 變成 DLL 後卸載不成功(Thread 的問題) |
缺席
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
由站上看到許多人都在使用 SPComm ,最近工作上的需求,原本使用同事開發的 RS232.dll 的模組,在 NT4.0 SP6 中竟然不能使用,只要改用 SPComm 自己重新包成 RS232.dll 的模組,以免修改原有的程式。
我的使用方式是
「主程式.exe」→「模組.dll」→「RS232.dll」 由主程式載入/卸載模組是使用動態連結,也就是使用 LoadLibrary(), GetProcAddress(), FreeLibrary() 等方式,模組則是靜態連結(RS232.lib)RS232.dll,在載入使用時,完全沒有問題,但是在主程式結束將所有模組卸載時,使用到 RS232.dll 的那個模組就會發生 Exception 。之所以認為是 SPComm 所作成之 RS232.dll 的問題,是因為在 Spcomm.pas:
procedure TComm.CloseReadThread; begin // If it exists... if ReadThread <> nil then begin // Signal the event to close the worker threads. SetEvent( hCloseEvent ); // Purge all outstanding reads PurgeComm( hCommFile, PURGE_RXABORT + PURGE_RXCLEAR ); // Wait 10 seconds for it to exit. Shouldn't happen. if (WaitForSingleObject(ReadThread.Handle, 10000) = WAIT_TIMEOUT) then ReadThread.Terminate; ReadThread.Free; ReadThread := nil end end; {TComm.CloseReadThread} procedure TComm.CloseWriteThread; begin // If it exists... if WriteThread <> nil then begin // Signal the event to close the worker threads. SetEvent(hCloseEvent); // Purge all outstanding writes. PurgeComm(hCommFile, PURGE_TXABORT + PURGE_TXCLEAR); FSendDataEmpty := True; // Wait 10 seconds for it to exit. Shouldn't happen. if WaitForSingleObject( WriteThread.Handle, 10000 ) = WAIT_TIMEOUT then WriteThread.Terminate; WriteThread.Free; WriteThread := nil end end; {TComm.CloseWriteThread}其中,註解的 「等10秒再離開,但不應該發生」的那個 WaitForSingleObject() 幾乎每次都發生了!!而在主程式卸載該模組時,則發生 Exception ,而 Exception 的指標是指到 Thread Terminate 函式非法存取。 同樣是使用 SPComm ,在之前測試時,使用的是 「測試程式.exe」→「RS232.dll」 使用靜態載入(RS232.lib)就不會有問題。 或許這個問題,不在 SPComm 的身上,但總是覺得怪怪的。 似乎在多層次的叫用有執行緒的 dll ,在動態卸載總是很容易發生 Thread Terminate 的 Exception,不知是否有類似的經驗或解決方法可以分享一下呢? 沒空更新的網頁...
------
http://www.ViewMove.com | ||||
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
目前似乎是找到問題,但是不能確定!!
可能是原本的 project 中使用了 LMD-Tools 的 ThreadTimer 元件。
因為個人對於 ThreadTimer 一直是又愛又恨,常常因為它,使得 Program Abnormal Terminated 的視窗常常出現,讓人覺得,這個軟體是不是有問題?!但是又不能不用,因為 Default 的 Timer 實在是太不準了。
最近去找一個有 source code 的 Thread Timer http://www.hellix.com/Products/TThreadedTimer.asp 而不再使用 LMD-Tools 的 ThreadTimer 後,就不再有 DLL 卸載不成功的問題了。 沒空更新的網頁...
------
http://www.ViewMove.com |
||||
AngusHuang
一般會員 發表:1 回覆:3 積分:0 註冊:2003-02-25 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |