Simple Async operation with Continuation scenario doesn't work on a WPF app

China☆狼群 提交于 2019-12-13 05:11:02

问题


I have a really simple operation on a WPF app in order to try out async operations. Here is my complete code:

static int GetPrimes() {

    var query =
        from n in Enumerable.Range(3, 5000000).AsParallel()
        where Enumerable.Range(2, (int)Math.Sqrt(n)).All(i => n % i > 0)
        select n;

    return query.Count();
}

private void button1_Click(object sender, RoutedEventArgs e) {

    Task<int> task = Task.Factory.StartNew(
        () => GetPrimes(),
        TaskCreationOptions.LongRunning
    );

    task.ContinueWith(x => {
        label1.Content = task.Result; 
    });
}

As you see, when I push the button it does some stuff and when it finishes it needs to push the result to a label but it doesn't do that.

I tested this synchronously and it worked.

What am I missing here?


回答1:


You don't explain what "it doesn't do that" means. I assume you're getting an exception because you're trying to manipulate a UI element from the wrong thread?

The problem is this code:

task.ContinueWith(x => {
    label1.Content = task.Result; 
});

You're not specifying which thread you want your continuation to run on, so it could be running on any thread. It's probably going to run on the same thread as your first task, i.e., a thread pool thread. Since you're not allowed to access UI from a thread other than the thread it was created on, that means your label1.Content assignment will fail with an exception.

The fix is simple: run your continuation on the UI thread. Change the above code to this:

task.ContinueWith(x => {
    label1.Content = task.Result; 
}, TaskScheduler.FromCurrentSynchronizationContext());



回答2:


Since you're working with background threads, you will need to marshal any code that plays with the UI to the UI thread.

task.ContinueWith(x => {
    Dispatcher.Invoke(() =>
        {
        label1.Content = task.Result; 
        },
        null);
});


来源:https://stackoverflow.com/questions/8423086/simple-async-operation-with-continuation-scenario-doesnt-work-on-a-wpf-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!