樓主,這個更好玩。用線程,不需要等待
unit u_SumEx;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
Tf_SumEx = class(TForm)
Panel1: TPanel;
edtSize: TEdit;
btnRunThread1: TButton;
Panel2: TPanel;
Memo1: TMemo;
Splitter1: TSplitter;
Memo2: TMemo;
btnRunThread2: TButton;
btnStopThread1: TButton;
lblArrayLength: TLabel;
lblMinValue: TLabel;
lblMaxValue: TLabel;
edtMinValue: TEdit;
edtMaxValue: TEdit;
lblResult: TLabel;
edtResult: TEdit;
procedure btnRunThread1Click(Sender: TObject);
procedure btnRunThread2Click(Sender: TObject);
procedure btnStopThread1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TSumThread=class(TThread)
private
FSub:array of Integer;
FLength:Integer;
FMin:Integer;
FMax:Integer;
FTmpSize:Integer;
FStrSum:String;
FStrList:TStrings;
FMemo:TMemo;
FResult:Integer;
procedure SumSum(iTmpSize:Integer)
//參數可以不要,而用私有變量代替嗎
procedure UpdateMemo;
public
constructor create(iLength,iMin,iMax,iResult:Integer;AMemo:TMemo);
destructor Destroy
override;
procedure execute;override;
end;
var
f_SumEx: Tf_SumEx;
ThreadOne:TSumThread;
ThreadTwo:TSumThread;
implementation
{$R *.dfm}
{ TSumThread }
constructor TSumThread.create(iLength, iMin, iMax,iResult: Integer;AMemo:TMemo);
begin
FLength:=iLength;
FMin:=iMin
//取值范圍最小值,如0
FMax:=iMax
//取值范圍最小值,如100
SetLength(Fsub,iLength);
FTmpSize:=0;
FStrList:=TStringList.Create;
FMemo:=AMemo
//指定在哪個Memo中顯示結果
FResult:=iResult
//結果等于多少時才顯示
inherited Create(False);
end;
destructor TSumThread.Destroy;
begin
FStrList.SaveToFile('c:/kkkaaa.txt')
//可惜只能得到最后一個線程的全部結果
FStrList.Free;
inherited;
end;
procedure TSumThread.execute;
begin
FreeOnTerminate:=True;
if FMin*FLength<=FResult then //小于或等于才開始,三個最小值不可能大于FResult
SumSum(FTmpSize);
end;
procedure TSumThread.SumSum(iTmpSize:Integer);
var
i, j, k: Integer;
s: string;
iSum: Integer;
begin
//這里的FOutSize和FTmpSize可分別用過程局部變量代替
//不過似乎顯得FOutSize和FTmpSize只在create時用了一次
for i := FMin to FMax do
begin
for j := 0 to FLength - 1 do
begin
FSub[iTmpSize] := i
//為哪個數賦值,假設這里是為第三個數賦值
end;
FTmpSize := iTmpSize + 1
//那么下一次就輪到為第四個數賦值了
iSum := 0;
if FTmpSize >= FLength then
begin
FStrSum:='';
for k := 0 to FLength - 1 do
begin
iSum := iSum + FSub[k];
FStrSum := FStrSum + IntToStr(Fsub[k]) + ' ';
end;
if iSum = FResult then
begin
FstrList.Add(FStrSum);
Synchronize(updatememo);
end;
end
else
begin
// OutSize:=iOutSize+1;
SumSum(FTmpSize);
end;
end;
end;
procedure Tf_SumEx.btnRunThread1Click(Sender: TObject);
begin
ThreadOne:=TSumThread.create(StrToInt(edtSize.Text),
StrToInt(edtMinValue.Text),
StrToInt(edtMaxValue.Text),
StrToInt(edtResult.Text),
f_SumEx.Memo1);
end;
procedure TSumThread.UpdateMemo;
begin
FMemo.Lines.Add(FStrSum);
end;
procedure Tf_SumEx.btnRunThread2Click(Sender: TObject);
begin
TSumThread.create(StrToInt(edtSize.Text),
StrToInt(edtMinValue.Text),
StrToInt(edtMaxValue.Text),
StrToInt(edtResult.Text),
f_SumEx.Memo2);
end;
procedure Tf_SumEx.btnStopThread1Click(Sender: TObject);
begin
if ThreadOne<>nil then
if btnStopThread1.Tag=0 then
begin
btnStopThread1.Tag:=1;
ThreadOne.Suspend;
end
else
begin
btnStopThread1.Tag:=0;
ThreadOne.Resume;
end;
end;
end.