如何創建多線程啊(請以下面的簡單Case舉個例子吧,謝謝)?(100分)

  • 主题发起人 主题发起人 lixin38
  • 开始时间 开始时间
L

lixin38

Unregistered / Unconfirmed
GUEST, unregistred user!
我要產生100000-999999的數字,想用9個線程來處理,分配如下:
線程1:產生100000-199999
線程2:產生200000-299999
線程3:產生300000-399999
.........
線程9:產生900000-999999
 
我随便写的。你看看
unit uThread1;
interface
uses
Classes,SysUtils;
type
TRomInt = class(TThread)
private
{ Private declarations }
FSInt:integer;
FEint:integer;
FList:TStrings;
protected
procedure Execute;
override;
public
Constructor Create(Memo:TStrings;SInt,Eint:Integer);
end;

implementation
{ 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 TRomInt.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TRomInt }
constructor TRomInt.Create(Memo:TStrings;SInt, Eint: Integer);
begin
FList:=Memo;
FSInt:=SInt;
FEint:=Eint;
inherited Create(True);
end;

procedure TRomInt.Execute;
var
i:integer;
begin
{ Place thread code here }
for i:=FSInt to FEintdo
begin
FList.Add(IntToStr(i));
if Terminated then
Exit;//停止将中断
end;
end;

end.
------调用--------
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
BitBtn1: TBitBtn;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses uThread1;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
T1,T2,T3:TRomInt;
begin
with TRomInt.Create(Memo1.Lines,10000,19999)do
begin
Resume;
end;
with TRomInt.Create(Memo2.Lines,20000,29999)do
begin
Resume;
end;
with TRomInt.Create(Memo3.Lines,30000,39999)do
begin
Resume;
end;
end;
 
暈 ̄用蓝叶菱的方法要消耗50%CPU,20200K的內存
有沒有改進的方法啊??
 
改进啊。。。主要是memo太消耗内存了。。
还没有想过,。。。。代码了。也就是循环了,
因为每次memo都要重画,不消耗才怪,不过你可以使用最后的PAINT就可以了,能少很多,但是还有没有好方法呢,问问高手了。
 
to 蓝叶菱:
那不用Memo,直接Write to Txt文本中如何?
 
来自:hityou, 时间:2006-7-6 11:36:11, ID:3497710
楼上不要老是叫蓝蓝了,让人认为是女的了。。
楼上可以这样,先写一个文件1.txt
var
f:textfile;
i:integer;
begin
assignfile(f,'c:/1.txt');
rewrite(f);
for i := 0 to 999999do
begin
writeln(f,inttostr(i));
end;
closefile(f);
showmessage('ok');
end;
 
你先放到TStringList,然后整体赋值给Memo就OK了
 
多人接受答案了。
 
后退
顶部