C# 异步线程启动和恢复同步

发布于 2024-12-11 12:03:19 字数 657 浏览 0 评论 0原文

我想异步启动 6 个线程,暂停它们并同步恢复...

它应该像这样工作:

  1. 线程 1 启动 (thr1.start())
  2. 线程 1 一些进展(从 txt 文件获取 mac 地址,初始化 com-对象)
  3. 线程 1 暂停(暂停,直到所有线程执行与线程 1 相同的操作)
  4. 线程 2 启动
  5. 线程 2 取得一些进展
  6. 线程 2 暂停
  7. 线程 3 启动
  8. 线程3 一些进展
  9. 线程 3 暂停
  10. ...
  11. 在所有 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:

  1. Thread 1 starting (thr1.start())
  2. Thread 1 some progress (getting mac-adresses from txt-file, initializing com-objects)
  3. Thread 1 paused (paused until all threads did the same as thread 1)
  4. Thread 2 starting
  5. Thread 2 some progress
  6. Thread 2 paused
  7. Thread 3 starting
  8. Thread 3 some progress
  9. Thread 3 paused
  10. ...
  11. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2024-12-18 12:03:20

您需要某种同步 - 每个都有一个 ManualResetEvent线程听起来很可能,具体取决于您的线程功能。


编辑:感谢您的更新 - 这是一个基本示例:

// initComplete is set by each worker thread to tell StartThreads to continue
//     with the next thread
//
// allComplete is set by StartThreads to tell the workers that they have all
//     initialized and that they may all resume


void StartThreads()
{
    var initComplete = new AutoResetEvent( false );
    var allComplete = new ManualResetEvent( false );

    var t1 = new Thread( () => ThreadProc( initComplete, allComplete ) );
    t1.Start();
    initComplete.WaitOne();

    var t2 = new Thread( () => ThreadProc( initComplete, allComplete ) );
    t2.Start();
    initComplete.WaitOne();

    // ...

    var t6 = new Thread( () => ThreadProc( initComplete, allComplete ) );
    t6.Start();
    initComplete.WaitOne();

    // allow all threads to continue
    allComplete.Set();
}


void ThreadProc( AutoResetEvent initComplete, WaitHandle allComplete )
{
    // do init

    initComplete.Set(); // signal init is complete on this thread

    allComplete.WaitOne(); // wait for signal that all threads are ready

    // resume all
}

请注意, 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:

// initComplete is set by each worker thread to tell StartThreads to continue
//     with the next thread
//
// allComplete is set by StartThreads to tell the workers that they have all
//     initialized and that they may all resume


void StartThreads()
{
    var initComplete = new AutoResetEvent( false );
    var allComplete = new ManualResetEvent( false );

    var t1 = new Thread( () => ThreadProc( initComplete, allComplete ) );
    t1.Start();
    initComplete.WaitOne();

    var t2 = new Thread( () => ThreadProc( initComplete, allComplete ) );
    t2.Start();
    initComplete.WaitOne();

    // ...

    var t6 = new Thread( () => ThreadProc( initComplete, allComplete ) );
    t6.Start();
    initComplete.WaitOne();

    // allow all threads to continue
    allComplete.Set();
}


void ThreadProc( AutoResetEvent initComplete, WaitHandle allComplete )
{
    // do init

    initComplete.Set(); // signal init is complete on this thread

    allComplete.WaitOne(); // wait for signal that all threads are ready

    // resume all
}

Note that the StartThreads method will block while the threads initialize - this may or may not be a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文