forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncActionExtensions.cs
More file actions
79 lines (67 loc) · 3.21 KB
/
Copy pathAsyncActionExtensions.cs
File metadata and controls
79 lines (67 loc) · 3.21 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Microsoft.PackageManagement.Internal.Utility.Async {
using System;
using System.Threading;
using System.Threading.Tasks;
internal static class AsyncActionExtensions {
public static T Timeout<T>(this T asyncAction, int seconds) where T : IAsyncAction {
asyncAction.Timeout = TimeSpan.FromSeconds(seconds);
return asyncAction;
}
public static T Responsiveness<T>(this T asyncAction, int seconds) where T : IAsyncAction {
asyncAction.Responsiveness = TimeSpan.FromSeconds(seconds);
return asyncAction;
}
public static T CancelWhen<T>(this T asyncAction, CancellationToken cancellationToken) where T : IAsyncAction {
cancellationToken.Register(asyncAction.Cancel);
return asyncAction;
}
public static T AbortWhen<T>(this T asyncAction, CancellationToken cancellationToken) where T : IAsyncAction {
cancellationToken.Register(asyncAction.Abort);
return asyncAction;
}
public static T Wait<T>(this T asyncAction) where T : IAsyncAction {
asyncAction.CompleteEvent.WaitOne();
return asyncAction;
}
public static T Wait<T>(this T asyncAction, int milliseconds) where T : IAsyncAction {
asyncAction.CompleteEvent.WaitOne(milliseconds);
return asyncAction;
}
public static T Wait<T>(this T asyncAction, TimeSpan timespan) where T : IAsyncAction {
asyncAction.CompleteEvent.WaitOne(timespan);
return asyncAction;
}
public static T OnCompletion<T>(this T asyncAction, Action onCompleteAction) where T : class, IAsyncAction {
asyncAction.OnComplete += onCompleteAction;
return asyncAction;
}
public static T OnCancellation<T>(this T asyncAction, Action onCancelAction) where T : class, IAsyncAction {
asyncAction.OnCancel += onCancelAction;
return asyncAction;
}
public static T OnAborted<T>(this T asyncAction, Action onAbortAction) where T : class, IAsyncAction {
asyncAction.OnAbort += onAbortAction;
return asyncAction;
}
public static Task<T> AsTask<T>(this IAsyncValue<T> asyncValue) {
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.None);
asyncValue.OnCancel += tcs.SetCanceled;
asyncValue.OnAbort += () => tcs.SetException(new Exception("aborted"));
asyncValue.OnComplete += () => tcs.SetResult(asyncValue.Value);
return tcs.Task;
}
}
}