Execute task on current thread

后端 未结 5 1046
灰色年华
灰色年华 2021-02-05 05:35

Is it possible to force a task to execute synchronously, on the current thread?

That is, is it possible, by e.g. passing some parameter to StartNew(), to ma

5条回答
  •  猫巷女王i
    2021-02-05 06:27

    Task scheduler decides whether to run a task on a new thread or on the current thread. There is an option to force running it on a new thread, but none forcing it to run on the current thread.

    But there is a method Task.RunSynchronously() which

    Runs the Task synchronously on the current TaskScheduler.

    More on MSDN.

    Also if you are using async/await there is already a similar question on that.

提交回复
热议问题