Jam*_*ght 3 multithreading xamarin.ios
几个星期后,我终于回到使用新的6.0版本更新我的MonoTouch iOS应用程序了.对于长时间运行的活动(我的应用程序使服务调用后台并进行图像上传),我使用的线程非常类似于示例.这一切都很有效,直到我升级.控制器中的典型模式如下所示:
protected void LogInButtonClicked(object sender, EventArgs e)
{
NetworkActivity.Start();
// start the request.
ThreadPool.QueueUserWorkItem ((cb) => {
var service = new ClientUserService();
var result = service.Login(this.UserName.Text, this.Password.Text);
// when done, switch back to UI.
this.InvokeOnMainThread (() => {
NetworkActivity.Stop();
// do the various other things to init the app on the UI thread.
}
});
}
Run Code Online (Sandbox Code Playgroud)
在5.x上工作得很好,从未崩溃过.它遵循公布的代码指南.但是现在我立即得到了UUIKit Consistency错误的例外:你正在调用一个只能从UI线程调用的UIKit方法.我在我的服务电话专线(service.Login(...))上得到了.
所以...我不确定我在这里做错了什么.我确实回过头来看一些较新的样本.其中一些使用任务库(例如https://github.com/xamarin/mobile-samples/blob/master/MultiThreading/iOSMultiThreading/Screens/MainScreen_iPhone.cs),但这不应排除此QueueUserWorkItem方法.
问题:Monotouch中的线程模型是否有重大变化,因此不再支持上述编码模式?
谢谢.JB
在InvokeOnMainThread委托中包装this.UserName.Text和this.Password.Text.Monotouch希望所有使用UI元素的工作都在UI线程中执行.