我想一个极为简单多线程的例子:
主线程在文本况中输入0-9中的一个数(1位),
另一个线程便复制同一个数显示在另一个文本框内,
给个提示,或是源码

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
     type
      TNumThread = class(TTHread)
      private
        FNum: Integer;
        procedure ShowData;
      protected
        procedure Execute; override;
      public
        ThreadNo: Integer;
      end;var
      lineCount: Integer;
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TNumThread.ShowData;
    begin
      Form1.Memo1.lines.Add(IntToStr(FNum) + ' ' + IntToStr(ThreadNo));
    end;
    procedure TNumThread.Execute;
    begin
      inherited;
      FreeOnTerminate := True;
      while lineCount < 10000 do
      begin
        InterlockedIncrement(lineCount);
        FNum := lineCount;
        // 同步
        Synchronize(ShowData);
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      numThread:array [0..4] of TNumThread ;
      I: Integer;
    begin
      memo1.Clear ;
      lineCount := 0;
      for I := 0 to 4 do
      begin
        numThread[I] := TNumThread.Create(true);
        numThread[I].ThreadNo := I;
        numThread[I].Resume ;
      end;
    end;end.参考一下这个
      

  2.   


    unit main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, myThread, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        ListBox1: TListBox;
        Label1: TLabel;
        StaticText1: TStaticText;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure OnThreadTerminate(Sender: TObject);
      end;var
      Form1: TForm1;
      th: array[0..4] of TThreadRandom;
      CS: TRTLCriticalSection;
      DoneFlags: Integer=0;
    implementation
    {$R *.dfm}{ TForm1 }procedure TForm1.OnThreadTerminate(Sender: TObject);
    begin
      inc(DoneFlags);
      listbox1.Items.Add((sender as TThreadRandom).ThreadName);
      if DoneFlags=5 then DeleteCriticalSection(CS);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      i: Integer;
    begin
      InitializeCriticalSection(CS);
      listbox1.Clear;
      memo1.Clear;
      for i:=0 to 4 do
        th[i]:=TThreadRandom.Create('Thread# ' + IntToStr(i));
    end;end.
    unit myThread;interfaceuses
      Classes, SysUtils, Windows;type
      TThreadRandom = class(TThread)
      private
        { Private declarations }
        FThreadName: String;
        procedure AddToMemo;
        procedure AddToList;
        procedure GetLines;
      protected
        procedure Execute; override;
      public
        property ThreadName: String Read FThreadName;
        constructor Create(threadName: String);overload;
        destructor Destroy; override;
      end;var
       FLines: Integer=0;implementation
    uses
      main;
    { 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 TThreadRandom.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ TThreadRandom }constructor TThreadRandom.Create(threadName: String);
    begin
      FreeOnTerminate:=true;
      OnTerminate:= form1.OnThreadTerminate;
      FThreadName:=threadName;
      inherited Create(false);
    end;procedure TThreadRandom.Execute;
    begin  Synchronize(GetLines);
      while(FLines<10000) do
      begin
        windows.EnterCriticalSection(CS);
        Synchronize(AddToMemo);
        Synchronize(GetLines);
        Windows.LeaveCriticalSection(CS);
      end;end;destructor TThreadRandom.Destroy;
    begin
      inherited Destroy;
    end;procedure TThreadRandom.AddToMemo;
    begin
      Randomize;
      form1.Memo1.Lines.Add(FThreadName + ' 添加:'+ IntToStr(Random(10000)));
    end;procedure TThreadRandom.AddToList;
    begin
      form1.ListBox1.Items.Add(FThreadName+' 已终止。');
    end;procedure TThreadRandom.GetLines;
    begin
      FLines:=form1.Memo1.Lines.Count;
      form1.StaticText1.Caption:=inttostr(FLines);
    end;end.program ThreadSyn;uses
      Forms,
      main in 'main.pas' {Form1},
      myThread in 'myThread.pas';{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    这两个例子参考一下