c#像目标c一样调度队列

我想模仿c#中objective-c调度队列的行为。 我看到有一个任务并行库,但我真的不明白如何使用它,并希望得到一些解释如何。

在客观ci中会做类似的事情:

-(void)doSomeLongRunningWorkAsync:(a_completion_handler_block)completion_handler { dispatch_async(my_queue, ^{ result *result_from_long_running_work = long_running_work(); completion_handler(result_from long_running_work); }); } -(void)aMethod { [self doSomeLongRunningWorkAsync:^(result *) { // the completion handler do_something_with_result_from_long_running_async_method_above; }]; } 

这怎么被翻译成c#style任务并行库?

任何比较网站?

如果你想要的只是在后台线程上执行一些长时间运行的CPU密集型代码,并且当它完成时,在UI线程上处理结果,使用Task.Run()结合await

 async Task AMethod() { var result = await Task.Run(() => LongRunningWork()); DoSomethingWithResult(result); } 

AMethod()现在是一个async Task方法,这意味着它的调用者也必须是async方法。