我感兴趣的是为什么我们需要调用InvokeOnMainThread,而这将是TaskScheduler.FromCurrentSynchronizationContext()的主要意图和责任。为什么不在Monotouch中同步TaskScheduler.FromCurrentSynchronizationContext?
我正在Monotouch中为iPhone应用程序使用TPL来完成一些后台任务并通过记者级更新UI。但是,似乎TaskScheduler.FromCurrentSynchronizationContext()并不像您所期望的那样与UI线程同步。此时,我通过使用Xamarin站点中Threading主题描述的InvokeOnMainThread,设法使其工作(但仍然感觉错误)。
我还发现在BugZilla上报告(类似)bug,似乎已解决..和另一个threading question关于在MonoTouch中使用后台线程的首选方法。
以下是用于说明我的问题和显示行为的代码片段。
private CancellationTokenSource cancellationTokenSource;
private void StartBackgroundTask()
{
this.cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = this.cancellationTokenSource.Token;
var progressReporter = new ProgressReporter();
int n = 100;
var uiThreadId = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine ("Start in thread " + uiThreadId);
var task = Task.Factory.StartNew (() =>
{
for (int i = 0; i != n; ++i) {
Console.WriteLine ("Work in thread " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep (30);
progressReporter.ReportProgress (() =>
{
Console.WriteLine ("Reporting in thread {0} (should be {1})",
Thread.CurrentThread.ManagedThreadId,
uiThreadId);
this.progressBar.Progress = (float)(i + 1)/n;
this.progressLabel.Text = this.progressBar.Progress.ToString();
});
}
return 42; // Just a mock result
}, cancellationToken);
progressReporter.RegisterContinuation (task,() =>
{
Console.WriteLine ("Result in thread {0} (should be {1})",
Thread.CurrentThread.ManagedThreadId,
uiThreadId);
this.progressBar.Progress = (float)1;
this.progressLabel.Text = string.Empty;
Util.DisplayMessage ("Result","Background task result: " + task.Result);
});
}
而记者类有这些方法
public void ReportProgress(Action action)
{
this.ReportProgressAsync(action).Wait();
}
public Task ReportProgressAsync(Action action)
{
return Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
public Task RegisterContinuation(Task task, Action action)
{
return task.ContinueWith(() => action(), CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
public Task RegisterContinuation<TResult>(Task<TResult> task, Action action)
{
return task.ContinueWith(() => action(), CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
在应用程序的输出窗口中的结果将是:
Start in thread 1
Work in thread 6
Reporting in thread 6 (should be 1)
Work in thread 6
Reporting in thread 6 (should be 1)
...
Result in thread 1 (should be 1)
正如你可以看到“螺纹6工作”是罚款。报告也在线索6上,这是错误的。有趣的是,RegisterContinuation
在线程1中进行了报告!
进展:我还没有想出这一个出来..任何人?
谢谢桑德尔,但我试过这个,它没有工作。它在控制台输出中给出相同的结果,UI中没有显示进度。正如我的问题所述,'ContinueWith()'似乎无需调用'InvokeOnMainThread()'即可工作。 – 2012-03-21 00:50:27
我不得不承认,我仅在Windows上使用WinForms项目尝试了此操作。在Windows上,控制台项目甚至不允许使用TaskScheduler.FromCurrentSynchronizationContext(),它会导致引发异常:“当前的SynchronizationContext不能用作TaskScheduler。” – 2012-03-21 08:51:12
@RemcoKoedoot,你能否展示你使用这种技术的更新代码(作为对问题的编辑)? – 2013-05-10 21:27:22