我做了一个线程程序,在这个线程里,我用一个循环来判断这个条件是否满足,当满足时就执行一个任务。可以现这样一个问题,创建了这个线程后。我再也不能操作这个界面上的所有东西,就像死机一样。当执行完任务后才能操作界面,请问有何好的办法解决此问题。先谢谢各位大侠。
代码如下:
线程文件:
unit Unit2;interfaceuses
  Classes;type
  testxc = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;implementation
{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure testxc.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ testxc }procedure testxc.Execute;
begin
  { Place thread code here }
end;end.
 单元文件
-----------------------------------
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons,unit2, ExtCtrls;type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    Edit1: TEdit;
    usedthread: TButton;
    nousedthread: TButton;
    Button1: TButton;
    Label1: TLabel;
    Timer1: TTimer;
    Label2: TLabel;
    procedure usedthreadClick(Sender: TObject);
    procedure nousedthreadClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    xc1,xc2 :testxc;
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}function textxcfunc(p:pointer):longint;stdcall;
begin
while form1.Edit1.Text<>TimeToStr(Time) do
begin
  form1.Label2.Caption:=TimeToStr(Time);
end;
end;
{function textxcfunc(p:pointer):longint;stdcall;
var
  i:integer;
  DC:HDC;
  S:string;
begin
  DC:=getdc(form1.Handle);
  for i:=0 to 1000000 do begin
      S:=inttostr(i);
      textout(DC,10,10,Pchar(S),length(S));
  end;
  releaseDC(form1.Handle,DC);
end;
}
procedure TForm1.usedthreadClick(Sender: TObject);
var
  hThread:thandle;
  threadid:dword;
begin
  hthread:=createthread(nil,0,@textxcfunc,nil,0,threadid);
  if hthread= 0 then messagebox(Handle,'didn`t create a thread',nil,MB_OK);
end;procedure TForm1.nousedthreadClick(Sender: TObject);
begin
textxcfunc(nil);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:=true;end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
textxcfunc(nil);
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
form1.Label1.Caption:=TimeToStr(Time);
if form1.Edit1.Text=TimeToStr(Time) then
  begin
  showmessage('ddd');
  timer1.Enabled:=false;
  end;
end;end.

解决方案 »

  1.   

    回为这个原因,我只好用timer实现,请各位高手不吝赐教。
      

  2.   

    看的我好晕,最好你先把注释去掉再贴出来;
    而且不知道你在哪创建线程的;
    另外线程不能直接使用界面的;因为你的.Execute中是空的,所以也无从判断是怎么回事
      

  3.   

    线程来直接使用某些界面是《Delphi开发人员指南》中提到的大忌。
    你还是想想有没有变通的方法吧。
      

  4.   

    你的程序我看得头大,不过没响应的话,你可以在长时间的处理中加上一条Application.ProcessMessage来改善,试试吧。
      

  5.   

    你的程序我懒得看了,不过你可以在你尝试在处理时加条Application.ProcessMessage可能会达到你的要求。