MonoTouch - Threading

The general task is to do something in the background thread, then when it is done, transfer the results to the user interface thread and tell the user.

I understand that there are two general ways:

I can use TPL:

var context = TaskScheduler.FromCurrentSynchronizationContext (); Task.Factory.StartNew (() => { DoSomeExpensiveTask(); return "Hi Mom"; }).ContinueWith (t => { DoSomethingInUI(t.Result); }, context); 

Or Old ThreadPool:

  ThreadPool.QueueUserWorkItem ((e) => { DoSomeExpensiveTask(); this.InvokeOnMainThread (() => { DoSomethingInUI(...); }); }); 

Is there a recommended way to use MonoTouch to create iOS apps?

+6
source share
2 answers

Although I prefer the syntax of the parallel task library , the ThreadPool code base is older (in Mono and MonoTouch), so you are likely to find documentation for it and are less likely to hit a bug .

+2
source

According to this document, mono touch provides access to ThreadPool and Thread:

MonoTouch runtime gives developers access to the .NET API streaming, both explicitly using threads (System.Threading.Thread, System.Threading.ThreadPool), and also implicitly using asynchronous delegate templates or BeginXXX methods.

http://docs.xamarin.com/ios/advanced_topics/threading

In addition, you must call InvokeOnMainThread to update your interface.

+1
source

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


All Articles