Execute task on current thread

后端 未结 5 1025
灰色年华
灰色年华 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条回答
  •  太阳男子
    2021-02-05 06:15

    You can simply return the result of func() wrapped in a Task.

    public class NoThreading : IThreads
    {
        public Task StartNew(Func func)
        {
            return Task.FromResult(func());
        }
    }
    

    Now you can attach "continue with" tasks to this.

提交回复
热议问题