Share via

How Close Async Operation before

Mauro Fantina 106 Reputation points
2020-08-18T08:08:53.753+00:00

Hello.

I'm developing a code that execute AsyncOperation: PairAsync

        private async  void Button_Click(object sender, RoutedEventArgs e)
        {
            // Device Picker
            var devicePicker = new DevicePicker();

            // Filter
            devicePicker.Filter.SupportedDeviceSelectors.Add(WiFiDirectDevice.GetDeviceSelector(WiFiDirectDeviceSelectorType.AssociationEndpoint));

            // Show
            var di = await devicePicker.PickSingleDeviceAsync(new Rect(new Point(0, 0), new Point(10, 10)));

            // Pair
            var result = di.Pairing.PairAsync();
        }

I want to Close the PairAsync operation after 20 seconds if it not completed. I don't know how to do this.

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments

Answer accepted by question author

Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,871 Reputation points
2020-08-18T12:46:12.397+00:00

Hello,

Welcome to Microsoft Q&A,

I want to Close the PairAsync operation after 20 seconds if it not completed. I don't know how to do this.

You could call AsTask extension method then pass 20s CancellationToken for PairAsync AsyncOperation method like the following.

var source = new CancellationTokenSource(TimeSpan.FromSeconds(20));  
var res = await PairAsync().AsTask(source.Token);  

Thanks
Nico Zhu

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Mauro Fantina 106 Reputation points
    2020-08-18T13:50:12.827+00:00

    But I have to dispose the CancellationTokenSource then ?

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.