11

我想先放代码,然后解释情况并据此提出我的问题:

public partial class MainWindow : Window {

    public MainWindow() {
        InitializeComponent();
    }

    private async void Button_Click_2(object sender, RoutedEventArgs e) {

        var result = await GetValuesAsync();
        Foo.Text += result;
    }

    public async Task<string> GetValuesAsync() {           

        using (var httpClient = new HttpClient()) {

            var response = await httpClient
                .GetAsync("http://www.google.com")
                .ConfigureAwait(continueOnCapturedContext: false);


            // This is the continuation for the httpClient.GetAsync method.
            // We shouldn't get back to sync context here
            // Cuz the continueOnCapturedContext is set to *false*
            // for the Task which is returned from httpClient.GetAsync method
            var html = await GetStringAsync();

            // This is the continuation for the GetStringAsync method.
            // Should I get back to sync context here?
            // Cuz the continueOnCapturedContext is set to *true*
            // for the Task which is returned from GetStringAsync 

            // However, GetStringAsync may be executed in another thread
            // which has no knowledge for the sync context 
            // because the continueOnCapturedContext is set to *false*
            // for the Task which is returned from httpClient.GetAsync method.

            // But, on the other hand, GetStringAsync method also has a 
            // chance to be executed in the UI thread but we shouldn't be
            // relying on that. 
            html += "Hey...";
            Foo.Text = html;

            return html;
        }
    }

    public async Task<string> GetStringAsync() {

        await Task.Delay(1000);
        return "Done...";
    }
}

这是一个相当简单的 WPF 示例,它在 .NET 4.5 上运行,可能没有多大意义,但这应该有助于我解释我的情况。

我在屏幕上有一个按钮,它有一个异步点击事件。查看GetValuesAsync代码时,您会看到await两次关键字的用法。第一次使用时,我将方法的continueOnCapturedContext参数设置为. 所以,这表明我不一定希望我的延续在. 到目前为止,一切都很好。Task.ConfigureAwaitfalseSynchronizationContext.Current

在第二次await使用(使用GetStringAsync方法)时,我没有调用该ConfigureAwait方法。所以,我基本上表示我回到当前的同步上下文以继续该GetStringAsync方法。因此,如您所见,我尝试TextBlock.Text在延续中设置(属于 UI 线程)属性。

当我运行应用程序并单击按钮时,我得到一个异常,给我以下消息:

调用线程无法访问此对象,因为不同的线程拥有它。

起初,这对我来说毫无意义,我认为我发现了一个错误,但后来,我意识到它GetStringAsync可能会在另一个不同于 UI 线程并且不知道同步上下文的线程中执行(很可能),因为continueOnCapturedContext设置falseTaskhttpClient.GetAsync方法返回的。

这是这里的情况吗?此外,在这种情况下,是否有机GetStringAsync会将方法发布回 UI 线程,因为 httpClient.GetAsync 方法的延续可能在 UI 线程内执行?

我在代码中也有一些评论。鉴于我的问题和代码中的评论,我在这里遗漏了什么吗?

4

1 回答 1

11

当您调用ConfigureAwait(false)时,该方法的其余部分将在线程池线程上执行,除非Task已经await完成。

由于GetAsync几乎肯定会异步运行,我希望GetStringAsync在线程池线程上运行。

public async Task<string> GetValuesAsync() {           

    using (var httpClient = new HttpClient()) {

        var response = await httpClient
            .GetAsync("http://www.google.com")
            .ConfigureAwait(continueOnCapturedContext: false);

        // And now we're on the thread pool thread.

        // This "await" will capture the current SynchronizationContext...
        var html = await GetStringAsync();
        // ... and resume it here.

        // But it's not the UI SynchronizationContext.
        // It's the ThreadPool SynchronizationContext.
        // So we're back on a thread pool thread here.

        // So this will raise an exception.
        html += "Hey...";
        Foo.Text = html;

        return html;
    }
}

此外,在这种情况下,是否有机会将 GetStringAsync 方法发回 UI 线程,因为 httpClient.GetAsync 方法的延续可能在 UI 线程内执行?

GetStringAsync在 UI 线程上运行的唯一方法是在实际编辑GetAsync之前完成。不可能。await

出于这个原因,我更喜欢ConfigureAwait(false)for every await once 不再需要上下文。

于 2012-10-17T11:44:46.517 回答