c#像目标c中那样调度队列
本文关键字:调度队列 目标 | 更新日期: 2023-09-27 18:19:29
我想模仿c#中objective-c调度队列的行为。我看到有一个任务并行库,但我真的不知道如何使用它,我希望能得到一些关于如何使用的解释。
在目标c中,我会做一些类似的事情:
-(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风格的任务并行库的?
有比较网站吗?
如果您只想在后台线程上执行一些长时间运行的CPU密集型代码,并且完成后在UI线程上处理结果,请将Task.Run()
与await
:结合使用
async Task AMethod()
{
var result = await Task.Run(() => LongRunningWork());
DoSomethingWithResult(result);
}
AMethod()
现在是async Task
方法,这意味着它的调用者也必须是async
方法。