以下內容僅供初學者參考
看到有的同學對delphi的執行緒認識不夠深,特開一貼給同學們講講。
主要給出兩種常用的執行緒形式。
1、長等待型執行緒示例,等待命令,執行不定長的工作,但每個工作的時間不會太長。
2、長工作型執行緒示例,執行乙個很長時間的工作,但可以很快響應取消操作。
以下程式所用的知識為:訊息機制以及常用的api函式
主程式unit1
delphi(pascal) code
unit unit1;inte***ce
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls,longwaittrd;
type
tform1
=class(tform)
btnsettask: tbutton;
btnexitthd: tbutton;
btncreatetrd: tbutton;
procedure
btnsettaskclick(sender: tobject);
procedure
btncreatetrdclick(sender: tobject);
procedure
btnexitthdclick(sender: tobject);
private
longwaitthread:tlongwaittrd;
procedure
onthreadmessage(
varmessage: tmessage); message wm_user
+2000
; public
end;
varform1: tform1;
implementation
procedure
tform1.btnsettaskclick(sender: tobject);
begin
if(longwaitthread
<>
nil)
then
postthreadmessage(longwaitthread.threadid,wm_user
+1000,0
,0);end
;procedure
tform1.btncreatetrdclick(sender: tobject);
begin
longwaitthread:
=tlongwaittrd.create(true);
longwaitthread.mainwin:
=handle;
longwaitthread.resume;
end;
procedure
tform1.onthreadmessage(
varmessage: tmessage);
begin
ifmessage.msg
=wm_user
+2000
then
begin
showmessage(string(message.lparam));
end;end
;procedure
tform1.btnexitthdclick(sender: tobject);
begin
if(longwaitthread
<>
nil)
then
beginif(
notlongwaitthread.exitlongwaittrd())
then
showmessage(
'the thread exited time out');
end;
end;
end.
執行緒類:
delphi(pascal) code
unit longwaittrd;inte***ce
uses
classes,windows,messages,syncobjs;
type
tlongwaittrd
=class(tthread)
private
fmainwin:thandle;
quitevent: tevent;
procedure
sendfeedbacktomainwin();
procedure
dothehardwork();
protected
procedure
execute; override;
public
constructor create(createsuspended: boolean);
destructor destroy; override;
function
exitlongwaittrd():boolean;
published
property mainwin:thandle read fmainwin write fmainwin;
end;implementation
uses unit1;
constructor tlongwaittrd.create(createsuspended: boolean);
begin
inherited create(createsuspended);
end;
destructor tlongwaittrd.destroy;
begin
inherited;
end;
procedure
tlongwaittrd.dothehardwork();
begin
//to
doend
;procedure
tlongwaittrd.execute;
varmsg: tmsg;
begin
freeonterminate:
=true;//1
.長等待型執行緒示例
//while
getmessage(msg, 0,
0, 0)
do//
begin
//if
(msg.message
=wm_user
+1000
) then
//任務來了
//begin
//dothehardwork();
//sendfeedbacktomainwin;
//end;//
if(msg.message
=wm_quit)
then
//begin
//break;
//end;//
end;//2
.長工作型執行緒示例
//while
(true)
do//
begin
//if
peekmessage(msg, 0,
0, 0, pm_remove)
then
//begin
//if
(msg.message
=wm_quit)
then
//begin
//break;
//end;//
end;
//dothehardwork();
//end
;
end;
function
tlongwaittrd.exitlongwaittrd;
begin
result:
=true;
quitevent:
=tevent.create(
nil,true,false,
'quitevent');
postthreadmessage(threadid,wm_quit,0,
0);if
(quitevent.waitfor(
2000)=
wrtimeout)
then
result:
=false;
quitevent.free ;
end;procedure
tlongwaittrd.sendfeedbacktomainwin();
varstatus:string;
begin
if(mainwin
<>0)
then
begin
status:='
the data has been processed by thread.';
postmessage(mainwin,wm_user
+2000,0
,integer(status))
end;
end;
end.
delphi 多執行緒
摘自 萬一的部落格 functionmyfun p pointer integer stdcall var i integer begin fori 0to500000do begin form1.canvas.lock form1.canvas.textout 10,10,inttostr i f...
DELPHI執行緒例子
優秀的資料庫應用應當充分考慮資料庫訪問的速度問題。通常可以通過優化資料庫 優化 查詢語句 分頁查詢等途徑收到明顯的效果。即使是這樣,也不可避免地會在查詢時閃現乙個帶有 sql符號的沙漏,即滑鼠變成了查詢等待。最可憐的是使用者,他 她 在此時只能無奈地等待。遇到急性子的,乾脆在此時嘗試 windows...
delphi 多執行緒
看別人的部落格一萬次記憶效果也沒那麼好,還是自己動手寫寫吧!functioncreatethread lpthreadattributes pointer dwstacksize dword lpstartaddress tfnthreadstartroutine lpparameter point...