title: Stephen Cleary Patterns for Asynchronous MVVM Applications: Data Binding- not working when executed from Command or properety setter
body: <p>I have spent my relaxed weekend playing around with the pattern suggested by Stephen Cleary:</p>
  
  <p><a href="https://msdn.microsoft.com/en-us/magazine/Dn605875.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/magazine/Dn605875.aspx</a></p>
  
  <p>As it works very fine in the example application, I havent seen it working when used in a Viewmodel method called from a ICommand bound to a Button.</p>
  
  <pre><code>        public NotifyTaskCompletion&lt;int&gt; Length { get; set; }
  
      private readonly ICommand _simpleCommand;
      public ICommand SimpleCommand
      {
          get { return _simpleCommand; }
      }      
  
      public MainViewModel()
      {
          _simpleCommand = new RelayCommand(Method);
      }
  
      public void Method(object o)
      {
          Length = new NotifyTaskCompletion&lt;int&gt;(MyStaticService.CountBytesInUrlAsync("http://www.example.com"));
      }
  </code></pre>
  
  <p>I've worked with a lot of debugging, and I can see that the PropertyChanged event is null when used as described above. If the same method <strong>Method</strong> is invoked from the constructor of the viewmodel, everything works fine.</p>
  
  <pre><code>public sealed class NotifyTaskCompletion&lt;TResult&gt; : INotifyPropertyChanged
  {
      public NotifyTaskCompletion(Task&lt;TResult&gt; task)
      {
          Task = task;
          if (!task.IsCompleted)
          {
              var _ = WatchTaskAsync(task);
          }
      }
      private async Task WatchTaskAsync(Task task)
      {
          try
          {
              await task;
          }
          // ReSharper disable once EmptyGeneralCatchClause
          catch (Exception e)
          {
          }
  
          var propertyChanged = PropertyChanged;
          if (propertyChanged == null)
          {
              return;
          }
  </code></pre>
  
  <p>Please let me know if anyone has an idea of the differences between the two scenarios.</p>