Q
qddmh
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个多线程程序如下:
主要是创建4个线程, 通过timer组件监控,当一个线程执行完毕后,重新创建。
有以下几个问题清讨论:
1。我的程序保持四个线程的方法是通过Timer组件监控。
2。释放线程后又重新创建。
我总感觉这种方法不好, 影响效率。
所以我想问
1。是否有其他方法来监控那个线程是否执行完?(例如消息控制?怎样做?)
2。能不能当一个线程执行完一个任务后,将它悬挂, 给它另一个任务后,在启动它,
如能应怎样做?这样就不用频繁的创建线成了。
3。我想问一下FlashGet的实现原理,也就是多线程多任务的解决方案。
欢迎大家讨论
美分了,
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, myThread1, ExtCtrls;
const
WM_ThreadDoneMsg = WM_User + 8;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Timer1: TTimer;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
StartThread: Boolean;
//避免开始创建窗体时Timer组件执行
AllDo: Boolean;
//停止线程标志
procedure ThreadDone(var AMessage : TMessage);
message WM_ThreadDoneMsg;
// Message to be sent back from thread when itsdo
ne
public
{ Public declarations }
end;
var
Form1: TForm1;
myThread: Array[0..3] of TMyThread;
ThreadActive: Array[0..3] of Boolean;
TaskNum: Integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to 3do
begin
if ((myThread = nil) or (ThreadActive = False)) then
begin
myThread := TmyThread.Create;
ThreadActive := True;
end
else
Showmessage('Thread Still Executing');
end;
StartThread := True;
AllDo := False;
Timer1.Enabled := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
for i := 0 to 4do
begin
ThreadActive := False;
myThread := nil;
end;
StartThread := False;
AllDo := False;
end;
procedure TForm1.ThreadDone(var AMessage: TMessage);
var
i: integer;
begin
for i := 0 to 3do
begin
if ((MyThread <> nil) and (MyThread.ThreadID = cardinal(AMessage.WParam))) then
ThreadActive := False;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i: integer;
begin
for i := 0 to 3do
begin
if (MyThread <> nil) and (ThreadActive = True) then
begin
MyThread.Terminate;
MyThread.WaitFor;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: integer;
begin
if AllDo = True then
begin
timer1.Enabled := False;
Exit;
end;
for i := 0 to 3do
begin
if (((myThread = nil) and (StartThread = True)) or ((ThreadActive = False) and (StartThread = True))) then
begin
myThread := TmyThread.Create;
ThreadActive := True;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
AllDo := True;
end;
end.
/////////////////////////////////////////////////////////////////////////////////
unit myThread1;
interface
uses
windows, Classes, comctrls, ShellAPI, Dialogs;
type
TMyThread = class(TThread)
private
procedure Showm;
protected
procedure Execute;
override;
public
constructor Create;
Destructor Destroy;
override;
end;
implementation
uses main;
{ MyThread }
constructor TMyThread.Create;
begin
inherited Create(True);
FreeOnTerminate := True;
Resume;
end;
destructor TMyThread.Destroy;
begin
PostMessage(form1.Handle,wm_ThreadDoneMsg,self.ThreadID,0);
inherited Destroy;
end;
procedure TMyThread.Execute;
begin
inherited;
Synchronize(Showm);
end;
procedure TMyThread.Showm;
begin
Form1.Memo1.Lines.Add('sss');
end;
end.
主要是创建4个线程, 通过timer组件监控,当一个线程执行完毕后,重新创建。
有以下几个问题清讨论:
1。我的程序保持四个线程的方法是通过Timer组件监控。
2。释放线程后又重新创建。
我总感觉这种方法不好, 影响效率。
所以我想问
1。是否有其他方法来监控那个线程是否执行完?(例如消息控制?怎样做?)
2。能不能当一个线程执行完一个任务后,将它悬挂, 给它另一个任务后,在启动它,
如能应怎样做?这样就不用频繁的创建线成了。
3。我想问一下FlashGet的实现原理,也就是多线程多任务的解决方案。
欢迎大家讨论
美分了,
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, myThread1, ExtCtrls;
const
WM_ThreadDoneMsg = WM_User + 8;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Timer1: TTimer;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
StartThread: Boolean;
//避免开始创建窗体时Timer组件执行
AllDo: Boolean;
//停止线程标志
procedure ThreadDone(var AMessage : TMessage);
message WM_ThreadDoneMsg;
// Message to be sent back from thread when itsdo
ne
public
{ Public declarations }
end;
var
Form1: TForm1;
myThread: Array[0..3] of TMyThread;
ThreadActive: Array[0..3] of Boolean;
TaskNum: Integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to 3do
begin
if ((myThread = nil) or (ThreadActive = False)) then
begin
myThread := TmyThread.Create;
ThreadActive := True;
end
else
Showmessage('Thread Still Executing');
end;
StartThread := True;
AllDo := False;
Timer1.Enabled := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
for i := 0 to 4do
begin
ThreadActive := False;
myThread := nil;
end;
StartThread := False;
AllDo := False;
end;
procedure TForm1.ThreadDone(var AMessage: TMessage);
var
i: integer;
begin
for i := 0 to 3do
begin
if ((MyThread <> nil) and (MyThread.ThreadID = cardinal(AMessage.WParam))) then
ThreadActive := False;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i: integer;
begin
for i := 0 to 3do
begin
if (MyThread <> nil) and (ThreadActive = True) then
begin
MyThread.Terminate;
MyThread.WaitFor;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: integer;
begin
if AllDo = True then
begin
timer1.Enabled := False;
Exit;
end;
for i := 0 to 3do
begin
if (((myThread = nil) and (StartThread = True)) or ((ThreadActive = False) and (StartThread = True))) then
begin
myThread := TmyThread.Create;
ThreadActive := True;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
AllDo := True;
end;
end.
/////////////////////////////////////////////////////////////////////////////////
unit myThread1;
interface
uses
windows, Classes, comctrls, ShellAPI, Dialogs;
type
TMyThread = class(TThread)
private
procedure Showm;
protected
procedure Execute;
override;
public
constructor Create;
Destructor Destroy;
override;
end;
implementation
uses main;
{ MyThread }
constructor TMyThread.Create;
begin
inherited Create(True);
FreeOnTerminate := True;
Resume;
end;
destructor TMyThread.Destroy;
begin
PostMessage(form1.Handle,wm_ThreadDoneMsg,self.ThreadID,0);
inherited Destroy;
end;
procedure TMyThread.Execute;
begin
inherited;
Synchronize(Showm);
end;
procedure TMyThread.Showm;
begin
Form1.Memo1.Lines.Add('sss');
end;
end.