次の方法で共有


Tasks are not Threads

One of the common misconceptions I’ve encountered when developers first start using the Task Parallel Library is that they think Tasks are just fancy threads. This is easy to assume because in a common case, calling Task.Run(…), it actually does run the Task activity on a thread from the thread pool. But as Stephen Toub wrote

The Task-based Async Pattern (TAP) isn’t just about asynchronous operations that you initiate and then asynchronously wait for to complete.  More generally, tasks can be used to represent all sorts of happenings, enabling you to await for any matter of condition to occur.

For example, let’s say I have a WPF app with two buttons, Start and End. The Start button’s event handler looks like this

    1: private async void bttnStart_Click(object sender, RoutedEventArgs e)
    2: {
    3:     MessageBox.Show("Looking for the time");
    4:     DateTime now = await GetCurrentTimeAsync();
    5:     MessageBox.Show("It is now" + now.ToString());
    6: }

which uses an async method to retrieve the current time. But we can implement GetCurrentTimeAsync() this like the following

    1: private void bttnEnd_Click(object sender, RoutedEventArgs e)
    2: {
    3:     if (_tsc != null)
    4:     {
    5:         _tsc.SetResult(DateTime.Now);
    6:     }
    7: }
    8:  
    9: TaskCompletionSource<DateTime> _tsc;
   10:  
   11: private Task<DateTime> GetCurrentTimeAsync()
   12: {
   13:     _tsc = new TaskCompletionSource<DateTime>();
   14:     return _tsc.Task;
   15: }

The Task returned from GetCurrentTimeAsync() will never complete until the user clicks the End button. In this case, the Task activity isn't code, but the act of waiting for the user to click the End button. In real code the activity could be happening on the network, the memory bus of the machine or anywhere, but it isn't necessarily on a thread.

In fact, Lucien Wischik has an excellent series on turning all sorts of things into Tasks so that they can be awaited.

Comments

  • Anonymous
    April 25, 2013
    Shouldn't your GetCurrentTimeAsync method check whether the _tsc field is already assigned before overwriting it? Otherwise, if you click the start button twice, you'll end up with a task that never completes.
  • Anonymous
    April 25, 2013
    The comment has been removed