關於多執行緒的語法...... |
答題得分者是:rexchiu
|
jojoboy
初階會員 發表:65 回覆:108 積分:34 註冊:2002-03-13 發送簡訊給我 |
小弟最近在學著Thread的使用....
所以找了幾個範例在練習.....
以下是我所練習的一個程式....
有一段宣告變數的的地方一直沒法子compile.....
所以放上來請教各方前輩.... unit Unit5; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls; type
TMainForm = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure ThreadsDone(Sender: TObject);
{ Public declarations }
end; var
MainForm: TMainForm; implementation uses Unit6; {$R *.DFM}
const
MaxSize = 128; var
NextNumber: Integer = 0;
DoneFlags: Integer = 0;
GlobalArray: array[1..MaxSize] of Integer;
//就是這段變數的宣告,應該使用在那呢??
因為以上的變數在MainForm與TThread裡都有使用到.....
procedure TMainForm.ThreadsDone(Sender: TObject);
var
i:Integer;
begin
Inc(DoneFlags);
if DoneFlags = 2 then
for i := 1 to MaxSize do
Listbox1.Items.Add(IntToStr(GlobalArray[i]));
end; procedure TMainForm.Button1Click(Sender: TObject);
begin
TFooThread.Create(False);
TFooThread.Create(False);
end; end.
--------------------------------------------------------------------
unit Unit6; interface uses
Windows, SysUtils, Classes, Controls, Dialogs; type
TFooThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end; implementation uses Unit5; { TFooThread }
function GetNextNumber: Integer;
begin
Result := NextNumber;
Inc(NextNumber);
end; procedure TFooThread.Execute;
var
i: Integer;
begin
{ Place thread code here }
OnTerminate := MainForm.ThreadsDone;
for i := 1 to MaxSize do
begin
GlobalArray[i] := GetNextNumber;
Sleep(5);
end;
end; end.
|
rexchiu
中階會員 發表:14 回覆:88 積分:70 註冊:2002-03-17 發送簡訊給我 |
把他寫成一個獨立的unit
然後有用到的unit,在use他.這樣應該就可以了! 或者你把那一段移到unit5的
略.. const MaxSize = 128; var MainForm: TMainForm; NextNumber: Integer = 0; DoneFlags: Integer = 0; GlobalArray: array[1..MaxSize] of Integer; //就是這段變數的宣告,應該使用在那呢?? 因為以上的變數在MainForm與TThread裡都有使用到..... implementation uses Unit6; {$R *.DFM} 略..Best Regards, Rex Chiu 發表人 - rexchiu 於 2005/02/22 15:52:38 發表人 - rexchiu 於 2005/02/22 15:55:31
------
Best Regards, Rex Chiu |
jojoboy
初階會員 發表:65 回覆:108 積分:34 註冊:2002-03-13 發送簡訊給我 |
謝謝前輩的幫忙......
改成這樣就可以了......
謝謝~~
unit Unit5; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls; type
TMainForm = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure ThreadsDone(Sender: TObject);
{ Public declarations }
end;
const
MaxSize = 128; var
MainForm: TMainForm;
NextNumber: Integer = 0;
DoneFlags: Integer = 0;
GlobalArray: array[1..MaxSize] of Integer;
implementation uses Unit6; {$R *.DFM}
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |