C# 异步线程启动和恢复同步
我想异步启动 6 个线程,暂停它们并同步恢复...
它应该像这样工作:
- 线程 1 启动 (thr1.start())
- 线程 1 一些进展(从 txt 文件获取 mac 地址,初始化 com-对象)
- 线程 1 暂停(暂停,直到所有线程执行与线程 1 相同的操作)
- 线程 2 启动
- 线程 2 取得一些进展
- 线程 2 暂停
- 线程 3 启动
- 线程3 一些进展
- 线程 3 暂停
- ...
- 在所有 6 个线程暂停之后,它们应该恢复全部..
我尝试使用 6 个简单的布尔标志并等到它们全部为 true,但这有点脏.. 有什么想法吗
?
编辑(更好的可视化):
Thr1 | Initiliazing |waiting |waiting | Resuming
Thr2 | waiting |Initiliazing |waiting | Resuming
Thr3 | waiting |waiting |Initiliazing | Resuming
...
谢谢和问候, 通量
i want to start 6 Threads asynchronously, paused them and resume synchronously...
it should work like this one:
- Thread 1 starting (thr1.start())
- Thread 1 some progress (getting mac-adresses from txt-file, initializing com-objects)
- Thread 1 paused (paused until all threads did the same as thread 1)
- Thread 2 starting
- Thread 2 some progress
- Thread 2 paused
- Thread 3 starting
- Thread 3 some progress
- Thread 3 paused
- ...
- after all 6 thread paused, they should resume all..
i tried with 6 simple boolean flags and wait until they are all true
but thats quite a bit dirty...
any ideas?
EDIT (better visualization):
Thr1 | Initiliazing |waiting |waiting | Resuming
Thr2 | waiting |Initiliazing |waiting | Resuming
Thr3 | waiting |waiting |Initiliazing | Resuming
...
thanks and greetz,
flux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要某种同步 - 每个都有一个 ManualResetEvent线程听起来很可能,具体取决于您的线程功能。
编辑:感谢您的更新 - 这是一个基本示例:
请注意, StartThreads 方法将在线程初始化时阻塞 - 这可能是问题,也可能不是问题。
You want some sort of synchronization - a ManualResetEvent for each thread sounds likely, depending on your thread functions.
EDIT: Thanks for your updates - here's a basic example:
Note that the
StartThreads
method will block while the threads initialize - this may or may not be a problem.