MonoTouch - 线程
一个常见的任务是在后台线程中执行某些操作,然后在完成后将结果传递给 UI 线程并通知用户。
我知道有两种常见的方法:
我可以使用 TPL:
var context = TaskScheduler.FromCurrentSynchronizationContext ();
Task.Factory.StartNew (() => {
DoSomeExpensiveTask();
return "Hi Mom";
}).ContinueWith (t => {
DoSomethingInUI(t.Result);
}, context);
或者旧的线程池:
ThreadPool.QueueUserWorkItem ((e) => {
DoSomeExpensiveTask();
this.InvokeOnMainThread (() => {
DoSomethingInUI(...);
});
});
使用 MonoTouch 构建 iOS 应用程序时是否有推荐的方法?
A common task is to do something in the background thread, then when done, pass the results to the UI thread and inform the user.
I understand there are two common ways:
I can use the TPL:
var context = TaskScheduler.FromCurrentSynchronizationContext ();
Task.Factory.StartNew (() => {
DoSomeExpensiveTask();
return "Hi Mom";
}).ContinueWith (t => {
DoSomethingInUI(t.Result);
}, context);
Or the Older ThreadPool:
ThreadPool.QueueUserWorkItem ((e) => {
DoSomeExpensiveTask();
this.InvokeOnMainThread (() => {
DoSomethingInUI(...);
});
});
Is there a recommended way to go when using MonoTouch to build iOS apps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我更喜欢 任务并行库
ThreadPool
代码库较旧(在 Mono 和 MonoTouch 中),因此您更有可能找到它的文档,并且不太可能遇到问题错误。While I prefer the syntax of Task Parallel Library the
ThreadPool
code base is older (in both Mono and MonoTouch) so you're more likely to find documentation for it and less likely to hit a bug.根据这个文档,mono touch提供了对ThreadPool和Thread的访问:
http://docs.xamarin.com/ios/advanced_topics/threading
此外,您应该调用 InvokeOnMainThread 来更新您的 UI。
According to this document, mono touch provides access to ThreadPool and Thread:
http://docs.xamarin.com/ios/advanced_topics/threading
Also, you should call InvokeOnMainThread to update your UI.