把程序简化一下,请大家帮忙看看:<br><br>unit Unit1a;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Edit1: TEdit;<br> Edit2: TEdit;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> procedure pro(var msg:TMsg);message WM_USER+100;<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br>implementation<br><br>{$R *.DFM}<br>uses Unit2;<br><br>var tthr:temp;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> if not Assigned(tthr) then<br> begin<br> hWnd:=handle;<br> IsTrue:=true;<br> tthr:=temp.Create(false);<br> end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> IsTrue:=false;<br> if Assigned(tthr) then<br> begin<br> tthr.Terminate; tthr:=nil;<br> end;<br>end;<br><br>procedure TForm1.pro(var msg:TMsg);<br>begin<br> Edit1.Text:=inttostr(strtoint(Edit1.Text)+1);<br> Edit2.Text:=inttostr(msg.wParam); //一直是0;<br>end;<br><br>end.<br><br>Unit2:<br>unit Unit2;<br><br>interface<br><br>uses<br> Classes, mmSystem, Messages, Forms, Windows;<br><br>type<br> temp = class(TThread)<br> private<br> { Private declarations }<br> protected<br> procedure Execute; override;<br> procedure FreeTimer(Sender: TObject);<br> end;<br><br>var hWnd:LongInt; tID,i:UINT; IsTrue:boolean;<br><br>implementation<br><br>{ Important: Methods and properties of objects in VCL can only be used in a<br> method called using Synchronize, for example,<br><br> Synchronize(UpdateCaption);<br><br> and UpdateCaption could look like,<br><br> procedure temp.UpdateCaption;<br> begin<br> Form1.Caption := 'Updated in a thread';<br> end; }<br><br>{ temp }<br>procedure temppro(uTimerID, uMessage: UINT;dwUser, dw1, dw2: DWORD); stdcall;<br>begin<br> Inc(i);<br> if i mod 10=0 then SendMessage(hWnd,WM_USER+100,i div 10,0);<br>end;<br><br>procedure temp.Execute;<br>var Mypro:TFNTimeCallBack;<br>begin<br> FreeOnTerminate:=true;<br> OnTerminate:=FreeTimer;<br> Mypro:=temppro;<br> tID:=TimeSetEvent(100,1,Mypro,0,1);<br> While IsTrue do Application.ProcessMessages;<br> { Place thread code here }<br>end;<br><br>procedure temp.FreeTimer(Sender: TObject);<br>begin<br> TimeKillEvent(tID);<br>end;<br><br>end.