【问题标题】:Running another function after thread finishes using ThreadStart线程完成后使用 ThreadStart 运行另一个函数
【发布时间】:2018-02-17 19:53:50
【问题描述】:

我有以下代码:

ThreadStart starter;
Thread worker_thread = null;

starter = Work;
starter += () => { AnotherWork();};
worker_thread = new Thread(starter) { IsBackground = true };
worker_thread.Start();

我想确保 AnotherWork 函数仅在 Work 函数执行完成后运行。但我不确定这段代码是否会按预期运行。

【问题讨论】:

标签: c# multithreading


【解决方案1】:

今天,C# 中的线程是一种遗留物,只是由于惯性而被添加到语言中。相反,您可以考虑使用 async/await 方法或 Task.ContinueWith()

我强烈推荐 Stephen Cleary 的“A Tour of Task”,一系列精彩的帖子详细描述了这一点:https://blog.stephencleary.com/2014/04/a-tour-of-task-part-0-overview.html

【讨论】:

    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多