NoneFrm.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
- unit NoneFrm;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- //定义一个线程类
- TsyncThread = class(TThread)
- protected
- procedure execute; override;
- end;
- TfrmNone = class(TForm)
- ListBox2: TListBox;
- ListBox1: TListBox;
- btnExecute: TButton;
- Label1: TLabel;
- Label2: TLabel;
- procedure btnExecuteClick(Sender: TObject);
- private
- //线程实例
- Thread1, Thread2: TSyncThread;
- //自定义过程,在线程结束时调用
- procedure Thread1Done(sender: TObject);
- procedure Thread2Done(sender: TObject);
- { Private declarations }
- public
- { Public declarations }
- end;
- const
- MaxSize = 100;
- var
- frmNone: TfrmNone;
- //数据全局区
- GlobalData: array[1..MaxSize] of integer;
- IncNum: Integer = 0;
- implementation
- {$R *.dfm}
- //线程执行主体
- procedure TSyncThread.execute;
- var
- i: integer;
- begin
- FreeOnTerminate := True;
- for i := 1 to Maxsize do
- begin
- Inc(IncNum);
- GlobalData[i] := IncNum;
- sleep(5);
- end;
- end;
- //创建线程实例
- procedure TfrmNone.btnExecuteClick(Sender: TObject);
- begin
- Thread1 := TSyncThread.Create(false);
- Thread1.OnTerminate := Thread1Done;
- Thread2 := TSyncThread.Create(false);
- Thread2.OnTerminate := Thread2Done;
- end;
- //当线程1执行完后,将GlobalData数据显示出来
- procedure TfrmNone.Thread1Done(sender: TObject);
- var
- i: integer;
- begin
- ListBox1.Items.Clear;
- for i := 1 to MaxSize do
- Listbox1.Items.Add(intToStr(GlobalData[i]));
- end;
- //当线程2执行完后执行 ,将GlobalData数据显示出来
- procedure TfrmNone.Thread2Done(sender: TObject);
- var
- i: integer;
- begin
- ListBox2.Items.Clear;
- for i := 1 to MaxSize do
- Listbox2.Items.Add(intToStr(GlobalData[i]));
- end;
- end.