C # send queues, as in lens c

I want to reproduce the behavior of objective-c dispatch queues in C #. I see that there is a parallel task library, but I really do not understand how to use it, and was hoping to get some explanation on how to do this.

In lens c, I would do something like:

-(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; }]; } 

How does this translate to a parallel C # style task library?

Any comparison sites?

+4
source share
1 answer

If all you need to do is execute some long-term code that uses CPU intensively in the background thread, and when this is done, process the result in the user interface thread, use Task.Run() in combination with await :

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

AMethod() now an async Task method, which means that its caller must also be an async method.

+1
source

Source: https://habr.com/ru/post/1487696/


All Articles