修改 Thread 成為元件的問題 |
尚未結案
|
bruce0211
版主 發表:157 回覆:668 積分:279 註冊:2002-06-13 發送簡訊給我 |
每次要用到 Thread 功能時,都得利用 Delphi專家(Expert)幫我產生一個給Thread 用的 Unit , 而我大部分關於 Thread 的應用又都是在 Thread 中做一個無窮迴圈(如 Do...While...)在迴圈裡面幫我做一些事情 我的想法是,是否可將 Thread 做成元件(叫做 TMyThread),替其加入一個叫做 OnThreadTimer 的事件,我把一些不希望讓 Application Hold 住的程式碼(如 comport input 偵測,排序等),丟到裡面即可,用法跟 TTimer 的 OnTimer 類似 , 差別只在 TTimer OnTimer 中的程式碼會讓 Application Hold 暫時 Hold 住,而放在 OnThreadTimer 的程式碼不會 , 但元件寫到下面這個階段就寫不下去了,希望有經驗的長官能指導一下...
< class="code">
unit MyThread; interface uses
SysUtils, Classes; type
TThreadTimer = procedure of object;
FThreadTimer:TThreadTimer; TmpThread = class(TThread)
private protected
procedure Execute; override;
public
constructor Create(AOwner : TComponent); override;
procedure ThreadTimer;
end; TMyThread = class(TComponent);
private
{ Private declarations }
AThread:TmpThread;
protected
{ Protected declarations }
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
public
{ Public declarations }
published
{ Published declarations }
property OnBroadcastMsg: TThreadTimer read FThreadTimer write FThreadTimer;
end; procedure Register; implementation //---------------------------------------------------------------------------
//
//---------------------------------------------------------------------------
procedure Register;
begin
RegisterComponents('MyWay', [TMyThread]);
end;
//---------------------------------------------------------------------------
constructor TMyThread.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
AThread:=TmpThread.Create;
AThread.FreeOnTerminate := True; { Start the thread. }
AThread.Resume; end;
//---------------------------------------------------------------------------
destructor TMyThread.Destroy;
begin
inherited Destroy;
//AThread.Free; if (AThread <> nil) and (not AThread.Terminated) then
begin
{ Tell the thread to terminate }
AThread.Terminate;
{ Wait until the thread actually terminates (this may not happen
straight away -- depends on what Execute is doing) }
repeat
until AThread.Terminated;
end; end;
//===========================================================================
constructor TmpThread.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
end;
//---------------------------------------------------------------------------
destructor TmpThread.Destroy;
begin
inherited Destroy;
end;
//---------------------------------------------------------------------------
procedure TmpThread.Execute;
begin
inherited Execute; while not Terminated do
begin
Synchronize(FThreadTimer);
Sleep(100);
end;
end; end. 發表人 -
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
建議您參考一下以下的元件,有原始碼可以參考:
http://www.hellix.com/Products/TThreadedTimer.asp
//////////////////////////////////////////////////////////////////////////////// // TThreadedTimer component - Heart 5.0 // // Component which allows to have an accurate timer (based on Delphi // Developper's Journal, issue of May 1996) // // by Hellix // products@hellix.com // http://www.hellix.com/ // // ?Copyright 1997-2001 ////////////////////////////////////////////////////////////////////////////////沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |