unit Unit2;interfaceuses
  Classes;type
  Tfoothread = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
    procedure threadsdone();
  end;
implementation
    uses unit1,SysUtils;
{ Important: Methods and properties of objects in VCL or CLX can only be used
  in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure Tfoothread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ Tfoothread }
    const
    maxsize=20;
    var
    nextnumber:integer=0;
    doneflags:integer=0;
    cs:trtlcriticalsection;    globalarray:array[1..maxsize] of integer;
    function getnextnumber:integer;
    begin
    result:=nextnumber;
    inc(nextnumber);
    end;
procedure Tfoothread.Execute;
var
i:integer;
beginfor i:=1 to maxsize do
 begin
 globalarray[i]:=getnextnumber; synchronize(threadsdone);
 end;  { Place thread code here }
end;
   procedure Tfoothread.threadsdone();
   var
   i:integer;
   j:integer;
    iCounter:integer;   begin
   InitializeCriticalSection(cs);
   icounter:=0;
    for i:=1 to 1000 do
    begin     EnterCriticalSection(cs);
           j:=i;
     form1.ListBox1.Items.Add(inttostr(j));
     LeaveCriticalSection(cs);    end;
    end;end.

解决方案 »

  1.   

    unit Unit2;interfaceuses
      Classes;type
      Tfoothread = class(TThread)
      private
        { Private declarations }
      protected
        procedure Execute; override;
        procedure threadsdone();
      end;
    implementation
        uses unit1,SysUtils;
    { Important: Methods and properties of objects in VCL or CLX can only be used
      in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure Tfoothread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ Tfoothread }
        const
        maxsize=20;
        var
        nextnumber:integer=0;
        doneflags:integer=0;
        cs:trtlcriticalsection;    globalarray:array[1..maxsize] of integer;
        function getnextnumber:integer;
        begin
        result:=nextnumber;
        inc(nextnumber);
        end;
    procedure Tfoothread.Execute;
    var
    i:integer;
    beginfor i:=1 to maxsize do
     begin
     globalarray[i]:=getnextnumber; synchronize(threadsdone);
     end;  { Place thread code here }
    end;
       procedure Tfoothread.threadsdone();
       var
       i:integer;
       j:integer;
        iCounter:integer;   begin
       InitializeCriticalSection(cs);
       icounter:=0;
        for i:=1 to 1000 do
        begin     EnterCriticalSection(cs);
               j:=i;
         form1.ListBox1.Items.Add(inttostr(j));
         LeaveCriticalSection(cs);    end;
        end;end.
      

  2.   

    是不是没有uses哪个单元文件?
      

  3.   

    我USES了呀,白日梦:你说什么位置不对,你说全嘛
      

  4.   

    声明临界区变量好象是这个吧:TCriticalSection
      

  5.   

    还有,你想要listbox显示出连续的i值吗?
    那你要在进循环之前就进入临界区
    在出循环之后出临界区
      

  6.   

    在implementation 
    前声明试试
      

  7.   

    //在窗体创建中
    InitializeCriticalSection(Critical1)
    //在窗体销毁中
    DeleteCriticalSection(Critical1)
    //在线程中
    EnterCriticalSection(Critical1)
    .........保护代码
    LeaveCriticalSection(Critical1)