線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2177
推到 Plurk!
推到 Facebook!

SPComm 變成 DLL 後卸載不成功(Thread 的問題)

缺席
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-12-15 13:15:50 IP:203.204.xxx.xxx 未訂閱
由站上看到許多人都在使用 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,不知是否有類似的經驗或解決方法可以分享一下呢? 沒空更新的網頁...
C及指標教學,計算機概論,資訊管理導論... http://coolsite.to/dllee 介紹Shells,LiteStep,GeoShell.... http://coolsite.to/ushells
------
http://www.ViewMove.com
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-12-26 14:20:08 IP:61.231.xxx.xxx 未訂閱
目前似乎是找到問題,但是不能確定!! 可能是原本的 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 卸載不成功的問題了。    沒空更新的網頁...
C及指標教學,計算機概論,資訊管理導論... http://coolsite.to/dllee 介紹Shells,LiteStep,GeoShell.... http://coolsite.to/ushells
------
http://www.ViewMove.com
AngusHuang
一般會員


發表:1
回覆:3
積分:0
註冊:2003-02-25

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-10-26 11:39:30 IP:203.204.xxx.xxx 未訂閱
版主 大大: 看到您已經成功將SPCOMM包裝成DLL,很是欽佩,因為我之前也嘗試這樣做,但是沒有成功,不知您是否可以將包裝的細節分享給我們初學者學習呢?如果方便的話,可否Email一份您的檔案給小弟?謝謝!! angus huang tenor@ms9.url.com.tw
系統時間:2024-04-25 22:10:00
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!