递归创建线程后,如何判断它们何时完成? [英] After recursively creating threads, how do I tell when they are done?

查看:209
本文介绍了递归创建线程后,如何判断它们何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows C#app中,我创建了一个线程,将主代码块

从GUI中分离出来,递归地对所有目录进行排序

从给定开始点,并且每次找到* .html时文件,

它启动一个新线程去处理该文件。由于每个

个别线程都不必触及另一个''s档,这个工作

罚款。


什么我需要知道所有这些线程何时完成。

理论上很多线程都可以被提示,我遇到了麻烦

搞清楚如何存储一个参考列表,所以我可以检查

他们的.IsAlive状态。


任何人都可以给我任何指示吗?

In my windows C# app, I create a thread to separate the main code block
from the GUI, which recursively sorts through all the directories
starting from a given point, and every time it finds a "*.html" file,
it starts a new thread to go to work on that file. Since each
individual thread doesn''t have to touch another''s file, this works
fine.

What I need to know is when all of these threads are finished.
Theoretically a lot of threads could be cued, and I''m having trouble
figuring out how to store a list of references to them so I can check
their ".IsAlive" state.

Can anyone give me any pointers?

推荐答案

David Beoulve< eg***********@yahoo.com>写道:
David Beoulve <eg***********@yahoo.com> wrote:
在我的Windows C#app中,我创建了一个线程,将主代码块与GUI分开,从给定的点开始递归排序所有目录
,每次发现* .html时文件,
它启动一个新的线程去上班。由于每个单独的线程都不必触及另一个''的文件,这很好用。


嗯......虽然听起来有点浪费。为什么不创建一个

的html文件列表来查看?机会正在增加额外的线程

这里不会帮助你 - 它只会增加复杂性。 (我不是

建议您在UI线程中完成工作 - 只需要创建几个线程,而不是每个文件一个)。

我需要知道的是所有这些线程都已完成。
理论上很多线程都可以被提示,而我很难解决如何存储列表对他们的引用所以我可以检查他们的.IsAlive任何人都可以给我任何指示吗?
In my windows C# app, I create a thread to separate the main code block
from the GUI, which recursively sorts through all the directories
starting from a given point, and every time it finds a "*.html" file,
it starts a new thread to go to work on that file. Since each
individual thread doesn''t have to touch another''s file, this works
fine.
Hmm... it sounds like a bit of a waste though. Why not just create a
list of html files to look through? Chances are adding extra threads
here isn''t going to help you - it''ll just add complexity. (I''m not
suggesting you do the work in the UI thread - just that you only create
a few threads, rather than one per file).
What I need to know is when all of these threads are finished.
Theoretically a lot of threads could be cued, and I''m having trouble
figuring out how to store a list of references to them so I can check
their ".IsAlive" state.

Can anyone give me any pointers?




好​​吧,你可以将线程引用添加到列表中,就像你要添加的一样任何

其他参考。您只需要访问一个公共列表。然而,不是使用IsAlive属性
,我会在

中的每个线程上调用Join,等待它完成。 (你可以在一个额外的

线程中执行此操作,然后告诉UI线程,一切都已完成,如果你想要

。)如果你只有一个但是,这可能不是必需的 - 你可能会发现将应用程序分成可能是

UI线程,搜索和线程和处理线程线程将

自然使生活更简单。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该组,请不要给我发邮件



Well, you can add Thread references to a list just like you''d add any
other reference. You just need access to a common list. Rather than
using the IsAlive property, however, I''d call Join on each thread in
turn to wait until it''s finished. (You could do this in a single extra
thread which then "told" the UI thread that everything had finished, if
you want.) If you only have a couple of threads, however, this may not
be necessary - you may well find that separating the app into maybe the
UI thread, the "searching" thread and the "processing" thread would
naturally make life simpler.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


尝试将它们放入Hashtable。

Peter


-

联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" David Beoulve"写道:
Try putting them into a Hashtable.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"David Beoulve" wrote:
在我的Windows C#app中,我创建了一个线程来将主代码块与GUI分开,后者递归地对所有目录进行排序
启动从给定的点开始,每次找到* .html时文件,
它启动一个新的线程去上班。由于每个单独的线程都不必触及另一个文件,因此工作正常。

我需要知道的是所有这些线程都完成之后。
从理论上讲,很多线索都可以被提示,而我很难找到如何存储一系列参考文献,以便我可以检查他们的.IsAlive。任何人都可以给我任何指示吗?
In my windows C# app, I create a thread to separate the main code block
from the GUI, which recursively sorts through all the directories
starting from a given point, and every time it finds a "*.html" file,
it starts a new thread to go to work on that file. Since each
individual thread doesn''t have to touch another''s file, this works
fine.

What I need to know is when all of these threads are finished.
Theoretically a lot of threads could be cued, and I''m having trouble
figuring out how to store a list of references to them so I can check
their ".IsAlive" state.

Can anyone give me any pointers?



大卫你好,

DB>在我的Windows C#app中,我创建了一个线程来分隔主代码

DB>从GUI中阻止,以递归方式对所有

DB>进行排序。从给定点开始的目录,每次找到一个

DB> " * HTML"。文件,它启动一个新线程去处理该文件。

DB>由于每个单独的线程都不必触及另一个''的文件,

DB>这样可以正常工作。


你是否考虑过该线程默认需要1mb的虚拟内存。

你最终可能会有很短的RAM限制在这个设计中。


---

WBR,

Michael Nemtsev ::博客: http://spaces.msn.com/laflour


"有时一个人仍然忠实于一个事业,只因为它的对手不会b / b不再是平淡的。 (c)Friedrich Nietzsche
Hello David,

DB> In my windows C# app, I create a thread to separate the main code
DB> block from the GUI, which recursively sorts through all the
DB> directories starting from a given point, and every time it finds a
DB> "*.html" file, it starts a new thread to go to work on that file.
DB> Since each individual thread doesn''t have to touch another''s file,
DB> this works fine.

Did u take into account that thread, by default, takes 1mb of virtual memory.
You can end up with RAM limit very shorty in this desing.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


这篇关于递归创建线程后,如何判断它们何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆