高分求一多线程例子[400分,先送上200](200分)

  • 主题发起人 主题发起人 eyas2
  • 开始时间 开始时间
E

eyas2

Unregistered / Unconfirmed
GUEST, unregistred user!
例子的功能是:
按了开始按钮后,有5个线程同时开始工作
第一个线程计算1+2+...+100
第二个线程计算100+101+...+200
类推.....
每个线程计算完成后,结果显示在主窗口中
我最想知道的是,5个线程如何同时进行工作,建立多个线程后,对应的每个线程的处理过程
应该怎么写?
请给我一个完完整整的例子。谢谢!如果分数不够还可以加!
 
去看Delphi目录下Demos/Threads/下的例子, 研究透, 你就知道怎么做了.
 
对不起,程序太大了,但基本是重复的部分,运行是只需要在窗体上放五个Label控件,
一个Button控件,其他均取默认值,将下列程序Copy进去,就能成功
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type
TMyThread1 = class(TThread)
published
procedure Execute;
override;
end;

type
TMyThread2 = class(TThread)
published
procedure Execute;
override;
end;

type
TMyThread3 = class(TThread)
published
procedure Execute;
override;
end;

type
TMyThread4 = class(TThread)
published
procedure Execute;
override;
end;

type
TMyThread5 = class(TThread)
published
procedure Execute;
override;
end;

var
Form1: TForm1;
MyThread1: TMyThread1;
MyThread2: TMyThread2;
MyThread3: TMyThread3;
MyThread4: TMyThread4;
MyThread5: TMyThread5;
implementation
{$R *.DFM}
procedure TMyThread1.Execute;
var i,j: Integer;
begin
j:=0;
for i:=1 to 10000do
begin
j:=j+i;
Form1.Label1.Caption:=IntToStr(j)
end;
end;

procedure TMyThread2.Execute;
var i,j: Integer;
begin
j:=0;
for i:=1 to 10000do
begin
j:=j+i;
Form1.Label2.Caption:=IntToStr(j)
end;
end;

procedure TMyThread3.Execute;
var i,j: Integer;
begin
j:=0;
for i:=1 to 10000do
begin
j:=j+i;
Form1.Label3.Caption:=IntToStr(j)
end;
end;

procedure TMyThread4.Execute;
var i,j: Integer;
begin
j:=0;
for i:=1 to 10000do
begin
j:=j+i;
Form1.Label4.Caption:=IntToStr(j)
end;
end;

procedure TMyThread5.Execute;
var i,j: Integer;
begin
j:=0;
for i:=1 to 10000do
begin
j:=j+i;
Form1.Label5.Caption:=IntToStr(j)
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
MyThread1:=TMyThread1.Create(False);//为False,直接运行
MyThread2:=TMyThread2.Create(False);
MyThread3:=TMyThread3.Create(False);
MyThread4:=TMyThread4.Create(False);
MyThread5:=TMyThread5.Create(False);
end;

end.
 
我已经编写了一个例子,代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
procedure OnThreadEnd(Sender: TObject);
public
{ Public declarations }
end;

TSumThread = class(TThread)
private
FStart,FEnd:integer;
protected
procedure Execute;
override;
public
sum:integer;
constructor Create(aStart, aEnd: integer;Suspended:boolean=false);
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
{ TSumThread }
constructor TSumThread.Create(aStart, aEnd: integer;Suspended:boolean=false);
begin
FStart:=aStart;
FEnd:=aend;
inherited Create(Suspended);
end;

procedure TSumThread.Execute;
var i:integer;
begin
sum:=0;
for i:=FStart to Fenddo
sum:=sum+i;
end;

procedure TForm1.OnThreadEnd(Sender: TObject);
begin
Memo1.lines.Add(IntToStr(TsumThread(Sender).Sum));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
aTh:array [0..3] of TSumThread;
i:integer;
begin
for i:=0 to 3do
begin
aTh:=TSumThread.Create(i*100+1,(i+1)*100,true);
aTh.FreeOnTerminate:=true;
aTh.OnTerminate:=OnThreadend;
ath.Resume;
end;
end;

end.
 
yzhshi: 程式有些问题。在Thread中不能直接对物件做更改。
必须调用下列的方式:
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure gg.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}

或者要使用
EnterCriticalSection();
Form1.Label2.Caption:=IntToStr(j);
LeaveCriticalSection();
 
谢谢你,CBX,我需要的就是这样的例子,我已经调试过了,已经明白了:)
还有200分一会儿送上,请你进去拿,ok?:)
 
后退
顶部