为什么我讲线程实例suspend后,代码执行不到    form1.Edit2.Text:=str+' ends';   LeaveCriticalSection(c);这里呢......
tthread执行了suspend就会直接退出了吗????procedure Ta.Execute;
var i:integer;
begin
  { Place thread code here }
  EnterCriticalSection(c);
  try
  form1.Edit2.Text:=str+' begins';
  while not suspended do
  begin
    form1.label1.caption:=str;
    i:=StrToInt(form1.Edit1.text);
    inc(i);
    form1.setCount(i);
    Sleep(1000); 
  end
  finally
    form1.Edit2.Text:=str+' ends';
    LeaveCriticalSection(c);
  end;
end;

解决方案 »

  1.   

    suspend,不是挂起吗,resume后,才继续执行.另外,在线程里面这样写是不对的:
    form1.Edit2.Text:=str+' begins'; 
    你应该用Synchronize同步函数.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 TMyThread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end;
      

  2.   

    有个疑问,你能在线程的execute里面写suspend后做什么吗?
    不是suspend后,线程都给挂起了,如何还能执行execute里面的代码
      

  3.   

    谢谢~已经明白可是如果我在form1中做一个函数修改edit1,同时在这个函数中用critical section,那是不是就可以不用Synchronize呢?
      

  4.   

    自己做同步会比较麻烦点,你要确信其他线程(包括主线程不会和你的线程有访问冲突),Synchronize的作用是把参数过程的代码放到主线程里执行.