关于多线程的重复执行(100分)

  • 主题发起人 主题发起人 olivetrees
  • 开始时间 开始时间
O

olivetrees

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了一个多线程的应用,但每个线程执行完后就不再执行,
我现在想要重复执行每个线程,用一个timer来触发,应该怎么办。
请大家帮我想我办法!!!!!!!
 
可以在线程执行代码用while循环,
执行完一次后suspend,需要再次执行时
用timer的事件触发resume.
 
呵呵,我也碰到你这样的问题,最后只好出笨招!
请看http://www.delphibbs.com/delphibbs/dispq.asp?lid=551628题中的源码
 
线程执行代码主体用while 重复循环,循环内最好加上sleep.
 
我觉得sleep不如suspend好,需要时在resume.
因为不知道什么时候需要再次使用
 
还有一个办法
在线程中使用WaitforSingleObject(HANDLE,INFINITE),那么线程会在这里阻塞
如果想再执行的话,只要SeEvent(HANDLE)
 
贴个例子,就是通过重复执行每个线程来测试线程级别的。
////////////////////////// 程序 //////////////////////////////////
unit ThreadFrmUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Bevel1: TBevel;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
Label3: TLabel;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
procedure FormShow(Sender: TObject);
private
Procedure MyThreadDown(Sender: TObject);
function SetPriority(const i:integer):TThreadPriority;
procedure SetButtonEnabled(const k:Integer);
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses ThreadUnit;
{$R *.DFM}
Var
MyThread1,MyThread2,Mythread3 :TMythread;
ThreadsRunning:integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
ThreadsRunning:=2;
MyThread1 :=TMyThread.Create(edit1,10000);
//建立线程1
with MyThread1do
begin
OnTerminate:=MyThreadDown;
Priority :=SetPriority(TrackBar1.Position);
//设置线程优先级别
end;

MyThread2 :=TMyThread.Create(edit2,10000);
//建立线程2
with MyThread2do
begin
OnTerminate:=MyThreadDown;
Priority :=SetPriority(TrackBar2.Position);
//设置线程优先级别
end;

SetButtonEnabled(0);
//设置按钮状态
end;

function TForm1.SetPriority(const i:integer):TThreadPriority;
//线程优先级别
begin
Result :=tpIdle;
Case i of
0: Result:=tpIdle;
1: Result:=tpLowest;
2: Result:=tpLower;
3: Result:=tpNormal;
4: Result:=tpHigher;
5: Result:=tpHighest;
6: Result:=tpTimeCritical;
end;
//case
end;

Procedure TForm1.MyThreadDown(Sender: TObject);
begin
dec(ThreadsRunning);
//线程减一
if ThreadsRunning=0 then
Form1.Button1.Enabled:=True;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
Label1.caption :='Thread1优先级别:'+IntToStr(TrackBar1.Position);
end;

procedure TForm1.TrackBar2Change(Sender: TObject);
begin
Label2.caption :='Thread2优先级别:'+IntToStr(TrackBar2.Position);
end;

Procedure Tform1.SetButtonEnabled(const k:integer);
//设置按钮状态
begin
if k=0 then
begin
button1.Enabled:=false;
Button2.enabled:=true;
Button3.enabled:=true;
// Button4.enabled:=True;
end else
begin
button1.Enabled:=true;
Button2.enabled:=False;
Button3.enabled:=false;
Button4.enabled:=false;
end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
SetButtonEnabled(1);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
MyThread1.Suspended :=True;
Button3.Enabled:=false;
Button4.Enabled:=True;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
MyThread1.Suspended :=False;
Button3.Enabled :=True;
Button4.Enabled:=false;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if MyThread1<>nil then
MyThread1.Terminate;
if MyThread2<>nil then
MyThread2.Terminate;
SetButtonEnabled(1);
end;

end.

////////////////////////// 引用的线程单元 ////////////////////////
unit ThreadUnit;
interface
uses
Classes,SysUtils,StdCtrls;
type
TMyThread = class(TThread)
private
{ Private declarations }
Aedit :TEdit;
MaxLoop,CurrentLoop:Integer;
protected
procedure Execute;
override;
procedure DispLoop;
Public
constructor Create(Edit:TEdit;
Max:integer);
end;

implementation

constructor TMyThread.Create(Edit:TEdit;Max:integer);
begin
Inherited Create(False);
AEdit :=Edit;
MaxLoop:=Max;
FreeOnTerminate:=True;
end;

procedure TMyThread.Execute;
var i:integer;
begin
for i:=0 to MaxLoopdo
begin
CurrentLoop:=i;
Synchronize(DispLoop);
if Terminated then
Exit;
end;
end;

procedure TMyThread.DispLoop;
begin
AEdit.text:=IntToStr(CurrentLoop);
end;

end.


 
接受答案了.
 
后退
顶部