为什么调用方法和异步方法具有相同的线程ID

时间:2016-01-06 16:03:15

标签: c# wpf multithreading asynchronous async-await

请参阅下面的代码。

public MainViewModel()
    {
        LongRunningOperationCommand = new RelayCommand(ExecuteLongRunningOperationCommand);
    }

    private void ExecuteLongRunningOperationCommand()
    {
        Test();
    }

    private async Task Test()
    {
        Log += "Command begin: " + DateTime.Now + "\r\n";
        Log += "Command thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
        var getStringAsync = GetStringAsync();
        Log += "Work in Command...\r\n";
        Log += "Work in Command which not related to the result of async method will complete: " + DateTime.Now + "\r\n";
        Log += "Work in Command which not related to the result of async method will complete, thread: " +
               Thread.CurrentThread.ManagedThreadId + "\r\n";
        string result = await getStringAsync;
        Log += "Command will complete: " + DateTime.Now + "\r\n";
        Log += "Command will complete, thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
        Log += result + "\r\n";
    }

    private async Task<string> GetStringAsync()
    {
        Log += "Async method begin: " + DateTime.Now + "\r\n";
        Log += "Async method thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
        Log += "Work in Async method... \r\n";
        await Task.Delay(10000);
        Log += "Async method will complete: " + DateTime.Now + "\r\n";
        Log += "Async method will complete, thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
        return "GetStringAsync method completed!";
    }

结果如下

Command begin: 1/6/2016 11:58:37 PM
Command thread: 8
Async method begin: 1/6/2016 11:58:37 PM
Async method thread: 8
Work in Async method... 
Work in Command...
Work in Command which not related to the result of async method will complete: 1/6/2016 11:58:37 PM
Work in Command which not related to the result of async method will complete, thread: 8
Async method will complete: 1/6/2016 11:58:47 PM
Async method will complete, thread: 8
Command will complete: 1/6/2016 11:58:47 PM
Command will complete, thread: 8
GetStringAsync method completed!

在GetStringAsync方法中等待Task.Delay之后的线程ID应该与之前不同。为什么结果是一样的?在控制台应用程序中,线程ID是不同的,但在WPF应用程序中,它们是相同的。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:5)

async / await的一个重点是,如果你有一个SynchronizationContext,就像WPF的DispatcherSynchronizationContext一样,那个线程上的工作将在await之后继续在该线程上,除非你告诉它到。

控制台应用程序没有SynchronizationContext,因此它使用默认上下文来调度线程池上的线程,这就是为什么你会看到WPF和控制台应用程序的不同行为。

要告诉async / await它不需要保持在等待时可以使用.ConfigureAwait(false)的同一个同步上下文,它将使用默认线程池上下文在需要时进行回调。

private async Task Test()
{
    Log += "Command begin: " + DateTime.Now + "\r\n";
    Log += "Command thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
    var getStringAsync = GetStringAsync();
    Log += "Work in Command...\r\n";
    Log += "Work in Command which not related to the result of async method will complete: " + DateTime.Now + "\r\n";
    Log += "Work in Command which not related to the result of async method will complete, thread: " +
           Thread.CurrentThread.ManagedThreadId + "\r\n";
    string result = await getStringAsync.ConfigureAwait(false);
    Log += "Command will complete: " + DateTime.Now + "\r\n";
    Log += "Command will complete, thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
    Log += result + "\r\n";
}

private async Task<string> GetStringAsync()
{
    Log += "Async method begin: " + DateTime.Now + "\r\n";
    Log += "Async method thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
    Log += "Work in Async method... \r\n";
    await Task.Delay(10000).ConfigureAwait(false);
    Log += "Async method will complete: " + DateTime.Now + "\r\n";
    Log += "Async method will complete, thread: " + Thread.CurrentThread.ManagedThreadId + "\r\n";
    return "GetStringAsync method completed!";
}

注意,执行.ConfigureAwait(false)并不保证代码的其余部分将在线程池上,如果任务处于Completed状态,代码将同步执行并保留在最初的任何线程上叫await

有关详细信息,请参阅artice“It's All About the SynchronizationContext”。

相关问题