| 发表于:2007-04-13 13:06:24 楼主 |
代码如下: 先执行 button2click 然后执行 button4click 为什么 线程没执行函数 没有推出去 但是 执行 button5click 后 isthreadterminate 明显时 true 的 拜求 高手执教 帮忙调试 unit unit1; interface uses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; type tthreadproc = procedure(const isthreadterminate : boolean) of object; tmythread = class(tthread) private fisthreadterminate: boolean; fthreadproc : tthreadproc; protected procedure EXECute; override; public constructor create(threadproc : tthreadproc);overload; destructor destroy;override; property isthreadterminate:boolean read fisthreadterminate write fisthreadterminate; procedure terminatethread; end; tform1 = class(tform) button1: tbutton; edit1: tedit; button2: tbutton; button3: tbutton; button4: tbutton; edit2: tedit; button5: tbutton; procedure button2click(sender: tobject); procedure button1click(sender: tobject); procedure button3click(sender: tobject); procedure button4click(sender: tobject); procedure button5click(sender: tobject); private mythread: tmythread; procedure num(const isthreadterminate : boolean); //procedure onthreaddestroy; public { public declarations } end; var form1: tform1; implementation {$r *.dfm} procedure tform1.num(const isthreadterminate : boolean); var i,j: integer; begin j:= 0; for i:= 0 to 100000 do begin if isthreadterminate then begin edit2.text:= '111111111111 '; break; end else edit2.text:= '2222222222222 '; j:= j + 1; edit1.text:= inttostr(j); end; end; procedure tmythread.EXECute; begin if assigned(fthreadproc) then fthreadproc(fisthreadterminate); end; constructor tmythread.create(threadproc : tthreadproc); begin fthreadproc:= threadproc; freeonterminate := true; fisthreadterminate:= false; inherited create(false); end; destructor tmythread.destroy; begin end; procedure tmythread.terminatethread; begin end; procedure tform1.button2click(sender: tobject); begin mythread:= tmythread.create(num); end; procedure tform1.button1click(sender: tobject); begin mythread.resume; end; procedure tform1.button3click(sender: tobject); begin mythread.suspend; end; procedure tform1.button4click(sender: tobject); begin mythread.suspend; //设置isthreadterminate 为true 让线程自动结束 mythread.isthreadterminate:= true; end; procedure tform1.button5click(sender: tobject); begin //测试 isthreadterminate 是否为true if mythread.isthreadterminate then showmessage( '111 '); end; end. |
|
|
|
|