Hi there,

 

Although this post is very informative, it didn’t quite fulfill my needs.

I needed to start an asynchronous method, lazy loading some data, when a property bound to a WPF control was updated.

This is what I came up with:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void UpdateInformation(string propertyValue)
{
    System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(new Action(async () =>
    {
        _cancellationTokens.ForEach(c => c.Cancel());
        using (CancellationTokenSource cancellationToken = new CancellationTokenSource())
        {
            try
            {
                _cancellationTokens.Add(cancellationToken);
                var dataResponse = await service.LoadData(propertyValue, cancellationToken);
                ...
            }
            finally
            {
                _cancellationTokens.Remove(cancellationToken);
            }
        }
    }));
}

As always, feel free to ask or comment.


Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading