从一个父线程中创建n个子线程,如何在这n个子线程都执行完成后通知父线程所有子线程已终结?

TonyTonyQ 2004-11-25 10:32:03
RT

比如(以下为伪代码):
-------------
dim father as new thread(addressof f)
father.start()
'do something else
-------------
public sub f()
for i as int32=0 to 100
dim chld as new thread(addressof child)
next
'do something else
end sub
-------------
public sub child()
'do something here, import codes here
end sub
-------------

如上所述,如何在这100个 chld 线程全部执行完成后通知父线程 father 并执行一定的操作?

另,由于父线程在创建子线程后有其自己的工作要做,所以不能用 Join 方法阻塞父线程。其实父线程在我的程序中是窗口UI线程,不能被阻塞,否则窗口就会无法刷新,像死机一样。我的目的就是要在所有子线程执行完成后通知窗口线程更改窗口状态。请不要告诉我使用control.Invoke方法,这个方法我知道,我想用标准线程来达到目的,请大虾指点。
...全文
393 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tjj3000 2004-11-29
  • 打赏
  • 举报
回复
我的方法:子线程中不用再开线程,利用异步委托实现,因为当你用代表发出调用请求后,CLR(公共语言运行环境,Common Language Runtime)接到这个请求,并将这个请求放置到一个内部的处理队列(Queue)中去


'定义委托
Delegate sub Dele_chld()
'定义回调函数
dim iCount as integer=0
private sub callbackchld(Byval ar as IAsyncResult)
icount=CInt(ar.AsyncState)
icount=iCount+1
if icount=100 then
dosomething
endif

end sub



dim father as new thread(addressof f)
father.start()
'do something else
-------------
public sub f()
for i as int32=0 to 100

dim Dele_chldInstance as new dele_chld (addressof child)
dim CallBackDelegate as new AsyncCallBack (AddressOf callbackchld)
dele_chldInstance.begininvoke((nothing,CallBackDelegate, 1)
next
end sub
public sub child()
'do something here, import codes here
end sub


没有调试过,只表明大概处理流程
asmallbee 2004-11-29
  • 打赏
  • 举报
回复
up
TonyTonyQ 2004-11-29
  • 打赏
  • 举报
回复
To: tjj3000(tomson)
你的想法是對的,我還是用綫程加委託實現了,謝謝,結貼。
gbbword 2004-11-26
  • 打赏
  • 举报
回复
用消息队列应该好一点吧。这个我也没有用过只是感觉。
delphi6net 2004-11-25
  • 打赏
  • 举报
回复
ding
TonyTonyQ 2004-11-25
  • 打赏
  • 举报
回复
我就是把子线程封装到类中來做的,然後結束后通過事件通知父綫程,但儅子綫程數大於1后,此方法的有效性不穩定(時而有作用,時而不起作用),所以覺得不適合用于多個子綫程。
haoztao 2004-11-25
  • 打赏
  • 举报
回复
将子线程封装到类当中就行了
捏造的信仰 2004-11-25
  • 打赏
  • 举报
回复
这个完全可以用类的事件来写呀。将子线程封装到类当中就行了。
兔子-顾问 2004-11-25
  • 打赏
  • 举报
回复
学习。
jxzhang615 2004-11-25
  • 打赏
  • 举报
回复
帮顶!
mycoldboy 2004-11-25
  • 打赏
  • 举报
回复
up

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧