Run an async function in another thread

后端 未结 3 673
深忆病人
深忆病人 2021-01-31 10:55

I\'m evaluating the Async CTP.

How can I begin execution of an async function on another thread pool\'s thread?

static async Task Test()
{
    // Do some         


        
3条回答
  •  星月不相逢
    2021-01-31 11:24

    It's usually up to the method returning the Task to determine where it runs, if it's starting genuinely new work instead of just piggy-backing on something else.

    In this case it doesn't look like you really want the Test() method to be async - at least, you're not using the fact that it's asynchronous. You're just starting stuff in a different thread... the Test() method could be entirely synchronous, and you could just use:

    Task task = TaskEx.Run(Test);
    // Do stuff
    t.Wait();
    

    That doesn't require any of the async CTP goodness.

提交回复
热议问题