




文檔簡介
(1)創建線程
MsgThread:=TMsgThread.Create(False);//創建并執行線程
MsgThread:=TMsgThread.Create(True);//創建線程后掛起
constructorCreate(CreateSuspended:Boolean);中的參數CreateSuspended表示創建后是否掛起線程。
(2)設置線程里沒有設置循環執行的話,且設置FreeOnTerminate為True,則線程執行完后就會自己釋放。
(3)在一個線程結束后,調用另一個事件的方法:
只要設置Onterminate:=某方法,這樣在線程結束前自然會被調用,比如:
procedureTSendShortMessageThread.Execute;
var
Bitmap:Tbitamp;
begin
Bimap:=Tbitmap.create(nil);
OnTerminate:=Threaddone;
end;
procedureThreaddone(sender:tobject);
begin
Bimap.Free;//在Destory之前會被調用
end;
(4)程序結束前安全的退出線程的方法:
ifMsgThread<>nilthen
begin
MsgThread.Terminate;
MsgThread.WaitFor;
end;
(5)判斷當前線程的狀態:
//以下資料來自大富翁論壇。
/判斷線程是否釋放
//返回值:0-已釋放;1-正在運行;2-已終止但未釋放;
//3-未建立或不存在
functionTFrmMain.CheckThreadFreed(aThread:TThread):Byte;
var
i:DWord;
IsQuit:Boolean;
begin
ifAssigned(aThread)then
begin
IsQuit:=GetExitCodeThread(aThread.Handle,i);
ifIsQuitthen//Ifthefunctionsucceeds,thereturnvalueisnonzero.
//Ifthefunctionfails,thereturnvalueiszero.
begin
ifi=STILL_ACTIVEthen//Ifthespecifiedthreadhasnotterminated,
//theterminationstatusreturnedisSTILL_ACTIVE.
Result:=1
else
Result:=2;//aThread未Free,因為Tthread.Destroy中有執行語句
end
else
Result:=0;//可以用GetLastError取得錯誤代碼
end
else
Result:=3;
end;
(6)線程同步。
如果線程要調用VCL里面的內容(如:別的窗體中的控件),就需要將這個線程同步。線程同步表示交由主線程運行這段代碼,各個線程都在主線程中分時間段運行。另外,要想避免多個線程同時執行同一段代碼也需要將多線程同步。
臨界區和互斥的作用類似,都是用來進行同步的,但它們間有以下一點差別:
臨界區只能在進程內使用,也就是說只能是進程內的線程間的同步;而互斥則還可用在進程之間的;臨界區所花消的時間很少,才10~15個時間片,而互斥需要400多個;臨界區隨著進程的終止而終止,而互斥,如果你不用closehandle()的話,在進程終止后仍然在系統內存在,也就是說它是系統全局對象;
同步的方法有:
(1)使用臨界區對象。
臨界區對象有兩種:TRTLCriticalSection和CriticalSection。
TRTLCriticalSection的用法
var
GlobalVariable:Double;
var
CriticalSection:TRTLCriticalSection;
procedureSetGlobalVariable(Value:Double);
begin
EnterCriticalSection(CriticalSection);//進入臨界區
try
GlobalVariable:=Value;
finally
LeaveCriticalSection(CriticalSection);//離開臨界區
end;
end;
initialization
InitializeCriticalSection(CriticalSection);//初始化
finalization
DeleteCriticalSection(CriticalSection);//刪除
end.
CriticalSection(重要區段)的用法:
varcriticalsection:TCriticalsection;
創建:criticalsection:=TCriticalsection.create;
使用:
criticalsection.enter;
try
...
finally
criticalsection.leave;
end;
(2)使用互斥
先在主線程中創建事件對象:
var
hMutex:THandle=0;
...
hMutex:=CreateMutex(nil,False,nil);
在線程的Execute方法中加入以下代碼:
ifWaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0then
//DoSomething;
...
ReleaseMutex(hMutex);
最后記得要釋放互斥對象:
CloseHandle(hMutex);
(3)使用信號量
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,
Dialogs,ExtCtrls,StdCtrls;
type
TForm1=class(TForm)
Edit1:TEdit;
Button1:TButton;
procedureFormCreate(Sender:TObject);
procedureFormDestroy(Sender:TObject);
procedureButton1Click(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
type
TMyThread=class(TThread)
private
protected
procedureExecute;override;
public
constructorCreate;virtual;
end;
var
Form1:TForm1;
HSem:THandle=0;
implementation
{$R*.dfm}
var
tick:Integer=0;
procedureTMyThread.Execute;
var
WaitReturn:DWord;
begin
WaitReturn:=WaitForSingleObject(HSem,INFINITE);
Form1.Edit1.Text:=IntToStr(tick);
Inc(tick);
Sleep(10);
ReleaseSemaphore(HSem,1,Nil)
end;
constructorTMyThread.Create;
begin
inheritedCreate(False);
FreeOnTerminate:=True;
end;
procedureTForm1.FormCreate(Sender:TObject);
begin
HSem:=CreateSemaphore(Nil,1,1,Nil);
end;
procedureTForm1.FormDestroy(Sender:TObject);
begin
CloseHandle(HSem);
end;
procedureTForm1.Button1Click(Sender:TObject);
var
index:Integer;
begin
forindex:=0to10do
begin
TMyTh
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中央企業投資合作協議書
- 品管部工作總結模版
- 大學生職業規劃大賽《審計學專業》生涯發展展示
- 有關消防試題及答案
- 應急消防面試試題及答案
- 生態攔截工程EPC項目投標技術方案
- 智慧城市運營管理方案
- 煙臺國企面試題庫及答案
- 商業地產停車位配比調整及使用權租賃合同
- 浙江省衢州市2025年八年級下學期語文期末考試卷及答案
- 科研項目風險管理與防范措施
- 2025年江西省三支一扶考試真題
- 青馬工程筆試題庫及答案
- 樂理考試題及答案2024
- Unit1 Making friends A Let's talk(教學設計)-2024-2025學年人教PEP版英語三年級上冊
- 2025年云南省文山州事業單位招聘歷年自考難、易點模擬試卷(共500題附帶答案詳解)
- 2025屆新高考物理沖刺復習:用動量定理解決帶電粒子在磁場中的運動問題
- 2025年春滬科版七年級數學下冊 第9章 分式 小結與復習
- 個稅贍養老人專項附加扣除分攤協議-綜合因素分攤
- 污水處理廠排水管道施工流程
- 《斷魂槍》老舍課件
評論
0/150
提交評論