编译出了问题,有劳各位给看一下:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation
uses
Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  Mytest1.Create(False);
end;end.
unit Unit2;interfaceuses
  Classes;type
  Mytest1 = 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 Mytest.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ Mytest }procedure Mytest1.Execute;
begin
FreeOnTerminate:=True;
  ShowMessage('线程1');
end;end.[Error] Unit2.pas(35): Undeclared identifier: 'ShowMessage'
[Fatal Error] Unit1.pas(28): Could not compile used unit 'Unit2.pas'

解决方案 »

  1.   

    unit Unit2; interface uses 
      Classes; type 
      Mytest1 = 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 Mytest.UpdateCaption; 
        begin 
          Form1.Caption := 'Updated in a thread'; 
        end; } { Mytest } procedure Mytest1.Execute; 
    begin 
    FreeOnTerminate:=True; 
      ShowMessage('线程1');    //这里出错了,这是第35句。
    end; end. 
      

  2.   

    是这样,改了(在Unit2单元加入Dialogs)以后编译可以通过了,可是这个线程运行后应该出现的对话框并没有出现。
      

  3.   

    对VCL的访问只能在主线程中进行,需要使用Synchronize方法? 
      

  4.   


    {VCL是非线程安全的,要Synchronize,不过还是不要在线程中直接使用VCL组件。
     至于你说的线程很复杂,有多复杂。很简单的,你不一定要使用TThread,也可以
     使用系统的CreateThread函数,虽然不推荐使用,但那只是VCL库中某些函数不能
     重入而导致错误的问题,同样的问题也在C语言库中。
     
     而且你发的帖子中用到了多线程,简直是驴头装到马屁股上,没用对地方。记住一点就好了
     线程就是自己开着一辆车,脱离大部队自己行动去了。不过把柄还在长官手里。就这个道理。
     
    }
      

  5.   

    在同步中是可以防问的,最终就是通过临界来实现的访问VCL, 对于强度较低的多线程程序,你这样写法是合适的(或者说是可以的)。 但对于处理强度高,VCL 交互太厉害,这就不适合了。
      

  6.   

    加上uses Dialogs;在工作线程中尽量少直接访问UI线程的东西