1 //--------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 //--------------------------------------------------------------------------
9 using System.ComponentModel;
11 namespace System.Threading.Tasks
13 internal class EAPCommon
15 internal static void HandleCompletion<T>(
16 TaskCompletionSource<T> tcs, AsyncCompletedEventArgs e, Func<T> getResult, Action unregisterHandler)
18 // Transfers the results from the AsyncCompletedEventArgs and getResult() to the
19 // TaskCompletionSource, but only AsyncCompletedEventArg's UserState matches the TCS
20 // (this check is important if the same WebClient is used for multiple, asynchronous
21 // operations concurrently). Also unregisters the handler to avoid a leak.
22 if (e.UserState == tcs)
24 if (e.Cancelled) tcs.TrySetCanceled();
25 else if (e.Error != null) tcs.TrySetException(e.Error);
26 else tcs.TrySetResult(getResult());