在我的一个线程中,我创建了几个子线程,然后我想在子线程都完成的情况下执行一些操作我下面的代码行吗?如果不行,请帮我指出来,最好给我一个类似的例子,
var
FEvent arrat[0..5] :Thandle;
ST arrat[0..5] :TMyread;
//创建传输线程 For I := 0 to TreadNum-1 do
begin
ST[i] := TMyread.Create(AAdrr,FPort) ;
FEvent[I] := ST[i].Handle ;
end; //等待线程完成
WaitForMultipleObjects(TreadNum, @ST, True, INFINITE);
var
FEvent arrat[0..5] :Thandle;
ST arrat[0..5] :TMyread;
//创建传输线程 For I := 0 to TreadNum-1 do
begin
ST[i] := TMyread.Create(AAdrr,FPort) ;
FEvent[I] := ST[i].Handle ;
end; //等待线程完成
WaitForMultipleObjects(TreadNum, @ST, True, INFINITE);
解决方案 »
- Delphi新人来报到下。放分啦。。。
- 为啥adostoredproc不能自动获得存储过程的参数
- TIdAttachmentFile是一个控件,还是类哦.我找不到!!!
- 如何让delphi编写的exe文件实现自动更新
- 128个2进制串转16进制
- 怎样用Delphi终止一DOS程序?
- 备注的打印问题
- 这里登记一下自己的网站,交流一下!
- shape + panel 想放2个shape在一个pannel上,新手
- 请大家帮个忙,我为此已好几天不思茶饭了:(
- 请问如何让mainmenu运行时候没有快捷健提示?
- 客户端使用SOCKETSVR.EXE 连接中间件服务器,只有本机能连上.其他机器均不能连接!服务端没有防火墙!各位大侠救命阿!
FEvent array[0..5] :Thandle;
ST array[0..5] :TMyread;
//创建传输线程 For I := 0 to TreadNum-1 do
begin
ST[i] := TMyread.Create(AAdrr,FPort) ;
FEvent[I] := WSACreateEvent();
ST[i].xxx ;= FEvent[I];//这样才可以!在TMyread里面要加一个public的WSAEVENT变量
end; //等待线程完成
WaitForMultipleObjects(TreadNum, @ST, True, INFINITE);
WaitForMultipleObjects(TreadNum, @FEvent[0], True, INFINITE);
然后子线程结束时SetEvent
所有子线程结束就会触发WaitForMultipleObjects(TreadNum, @FEvent[0], True, INFINITE);了
在TMyread里面要加一个public的WSAEVENT变量 ,这个变量为Thandle类型的就可以了吗?
YES
大侠,能详细解析下下面的几句什么作用么?看不懂啊,thanks ST[i] := TMyread.Create(AAdrr,FPort) ;
FEvent[I] := WSACreateEvent();
ST[i].xxx ;= FEvent[I];//这样才可以!在TMyread里面要加一个public的WSAEVENT变量
unit Unit2;
interfaceusesClasses, Windows,winsock,shellapi;//"Worker thread declaration"typeTTestThr = class(TThread)private{ Private declarations }protectedprocedure Execute; override;
Public
Event:Thandle;end;//Wait thread declarationtypeTWaitThr = class(TThread)privateprocedure UpdateLabel;//this just sets the label's captionprotectedprocedure Execute; override;end;implementationuses unit1;{ TTestThr }procedure TTestThr.Execute;varI : Integer;beginFreeOnTerminate := True;for I := 0 to 39 doSleep(500);SetEvent(Event);end;procedure TWaitThr.UpdateLabel;beginForm1.Label1.Caption := 'Not Waiting';end;procedure TWaitThr.Execute;varhndlArr : Array[0..4] of THandle;thrArr : Array[0..4] of TTestThr;I : Integer;beginFreeOnTerminate := True;for I := 0 to 4 do begin
begin thrArr[I] := TTestThr.Create(False);
hndlArr[I]:= CreateEvent(nil, True, False, nil);
thrArr[I].Event := hndlArr[I];
end;//Sleep(1000); //stagger creation of the threads
end;WaitForMultipleObjects(5, @thrArr[0] , false, INFINITE);Synchronize(UpdateLabel);
end;end.
unit1为:
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := 'Waiting';
Label1.Refresh ;TWaitThr.Create(False);end;
我对程序进行了跟踪,发现主程序并没有等待子程序,而是不管子程序是否完成,就马上进行下去了
flyinwuhan(制怒·三思而后行) 我错在那里?
WaitForMultipleObjects(5, @thrArr[0] , false, INFINITE);
改为
WaitForMultipleObjects(5, @thrArr[0] ,true, INFINITE);
也不行
interface
usesClasses, Windows,winsock,shellapi;//"Worker thread declaration"typeTTestThr = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
Public
Event:Thandle;
end;//Wait thread declarationtypeTWaitThr = class(TThread)privateprocedure UpdateLabel;//this just sets the label's captionprotectedprocedure Execute; override;end;implementationuses unit1;{ TTestThr }procedure TTestThr.Execute;
var
I : Integer;
begin
FreeOnTerminate := True;
for I := 0 to 39 do
Sleep(500);
SetEvent(Event);
end;procedure TWaitThr.UpdateLabel;
begin
Form1.Label1.Caption := 'Not Waiting';
end;procedure TWaitThr.Execute;
var
hndlArr : Array[0..4] of THandle;
thrArr : Array[0..4] of TTestThr;
I : Integer;
begin
FreeOnTerminate := True;
for I := 0 to 4 do begin
begin
thrArr[I] := TTestThr.Create(False);//这里要改成true,先让它休眠!先不执行!
hndlArr[I]:= CreateEvent(nil, True, False, nil);
thrArr[I].Event := hndlArr[I];
thrArr[I].resume;//等事件句柄传递过去再唤醒这个线程
end;//Sleep(1000); //stagger creation of the threads
end;WaitForMultipleObjects(5, @thrArr[0] , true, INFINITE);Synchronize(UpdateLabel);
end;end.
unit Unit2;
interfaceusesClasses, Windows,winsock,shellapi;//"Worker thread declaration"typeTTestThr = class(TThread)private{ Private declarations }protected
constructor Create(ANum:integer); virtual;
procedure Execute; override;
Public
FHandle:integer;end;//Wait thread declarationtypeTWaitThr = class(TThread)privateprocedure UpdateLabel;//this just sets the label's captionprotectedprocedure Execute; override;end;
varhndlArr : Array[0..4] of THandle;implementationuses unit1;{ TTestThr }constructor TTestThr.Create(ANum: integer);
begin
inherited Create(false);
end;procedure TTestThr.Execute;varI : Integer;beginFreeOnTerminate := True;for I := 0 to 39 do
Sleep(50);SetEvent(hndlArr[Fhandle]);
end;procedure TWaitThr.UpdateLabel;beginForm1.Label1.Caption := 'Not Waiting';end;procedure TWaitThr.Execute;varthrArr : Array[0..4] of TTestThr;I : Integer;beginFreeOnTerminate := True;for I := 0 to 4 do begin
begin thrArr[I] := TTestThr.Create(1);
hndlArr[I]:= CreateEvent(nil, True, False, nil);
thrArr[I].FHandle := i;
end;end;
WaitForMultipleObjects(5, @hndlArr , true, INFINITE);
Synchronize(UpdateLabel);
end;end.
不能Wait TThread,只能Wait 线程的句柄.
代码如:
var
Handles: array[0..5] of THandle;
begin
For I := 0 to TreadNum-1 do begin
with TMyread.Create(AAdrr,FPort) do begin
FreeOnTerminate:=true;
Handles[i] := Handle ;
Resume; //最好生成时Suspended
end;
End;
...
//等待线程完成
WaitForMultipleObjects(TreadNum, @Handles[0], True, INFINITE);
//也可以用MsgWaitForMultipleObjects,
//这样只要向执行Wait的线程Post一个消息就可以中止Wait.
Change notification
Console input
Event
Mutex
Process
Semaphore
Thread---〉 A thread object's state is signaled when the thread terminates.
Timer
是没有必要CreatEvent.