-
Notifications
You must be signed in to change notification settings - Fork 0
/
EAPCommon.cs
31 lines (29 loc) · 1.2 KB
/
EAPCommon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//--------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: EAPCommon.cs
//
//--------------------------------------------------------------------------
using System.ComponentModel;
namespace System.Threading.Tasks
{
internal class EAPCommon
{
internal static void HandleCompletion<T>(
TaskCompletionSource<T> tcs, AsyncCompletedEventArgs e, Func<T> getResult, Action unregisterHandler)
{
// Transfers the results from the AsyncCompletedEventArgs and getResult() to the
// TaskCompletionSource, but only AsyncCompletedEventArg's UserState matches the TCS
// (this check is important if the same WebClient is used for multiple, asynchronous
// operations concurrently). Also unregisters the handler to avoid a leak.
if (e.UserState == tcs)
{
if (e.Cancelled) tcs.TrySetCanceled();
else if (e.Error != null) tcs.TrySetException(e.Error);
else tcs.TrySetResult(getResult());
unregisterHandler();
}
}
}
}