-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncTask.cs
135 lines (102 loc) · 6.27 KB
/
AsyncTask.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#region Copyright (C) 2017-2021 Starflash Studios
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License (Version 3.0)
// as published by the Free Software Foundation.
//
// More information can be found here: https://www.gnu.org/licenses/gpl-3.0.en.html
#endregion
#region Using Directives
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.Foundation;
using Microsoft.VisualStudio.Shell;
using Task = System.Threading.Tasks.Task;
#endregion
namespace QMediaVSIX {
[AsyncMethodBuilder(typeof(ATaskMB))]
public class AsyncTask : Task {
public AsyncTask( Action Ak = null ) : base(Ak ?? (() => { })) { }
public async Task InvokeAsync() => await ConfigureAwait(false);
public void Invoke(bool Block = false) {
if (Block) {
ThreadHelper.JoinableTaskFactory.Run(async () => await InvokeAsync());
} else {
ThreadHelper.JoinableTaskFactory.RunAsync(async () => await InvokeAsync());
}
}
[SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "Operator Name")]
public static implicit operator AsyncTask(Func<Task> Lambda) => (AsyncTask)Lambda.Invoke();
public static implicit operator Func<Task>(AsyncTask ATask) => async () => await ATask.InvokeAsync().ConfigureAwait(false);
[SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "Operator Name")]
public static implicit operator AsyncTask( Delegate Del ) => new AsyncTask(() => Del.DynamicInvoke());
}
public static class AsyncTask_Extensions {
[SuppressMessage("Usage", "VSTHRD003:Avoid awaiting foreign Tasks", Justification = "<Pending>")]
public static void Invoke(this Task T, bool Block = false) {
if (Block) {
ThreadHelper.JoinableTaskFactory.Run(async () => await T.ConfigureAwait(false));
} else {
ThreadHelper.JoinableTaskFactory.RunAsync(async () => await T.ConfigureAwait(false));
}
}
//public static void Invoke(this Action Ak, bool Block = false) => new AsyncTask(Ak).Invoke(Block);
//public static async Task InvokeAsync(this Action Ak) => await new AsyncTask(Ak).InvokeAsync();
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "<Pending>")]
//public static AsyncTask<T> GetAsyncTask<T>( this IAsyncOperation<T> Op ) => AsyncTask<T>.GetAsyncTask(Op);
}
public class ATaskMB<T> {
AsyncTaskMethodBuilder<T> _MB;
public ATaskMB() => _MB = AsyncTaskMethodBuilder<T>.Create();
public static ATaskMB<T> Create() => new ATaskMB<T>();
public void Start<TStateMachine>( ref TStateMachine StateMachine ) where TStateMachine : IAsyncStateMachine => _MB.Start(ref StateMachine);
public void SetStateMachine( IAsyncStateMachine StateMachine ) => _MB.SetStateMachine(StateMachine);
public void SetException( Exception Exception ) => _MB.SetException(Exception);
public void SetResult( T Result ) => _MB.SetResult(Result);
public void AwaitOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter Awaiter, ref TStateMachine StateMachine )
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine => _MB.AwaitOnCompleted(ref Awaiter, ref StateMachine);
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter Awaiter, ref TStateMachine StateMachine )
where TAwaiter : ICriticalNotifyCompletion
where TStateMachine : IAsyncStateMachine => _MB.AwaitUnsafeOnCompleted(ref Awaiter, ref StateMachine);
public AsyncTask<T> Task => _MB.Task as AsyncTask<T>;
}
public class ATaskMB {
AsyncTaskMethodBuilder _MB;
public ATaskMB() => _MB = AsyncTaskMethodBuilder.Create();
public static ATaskMB Create() => new ATaskMB();
public void Start<TStateMachine>( ref TStateMachine StateMachine ) where TStateMachine : IAsyncStateMachine => _MB.Start(ref StateMachine);
public void SetStateMachine( IAsyncStateMachine StateMachine ) => _MB.SetStateMachine(StateMachine);
public void SetException( Exception Exception ) => _MB.SetException(Exception);
public void SetResult() => _MB.SetResult();
public void AwaitOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter Awaiter, ref TStateMachine StateMachine )
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine => _MB.AwaitOnCompleted(ref Awaiter, ref StateMachine);
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter Awaiter, ref TStateMachine StateMachine )
where TAwaiter : ICriticalNotifyCompletion
where TStateMachine : IAsyncStateMachine => _MB.AwaitUnsafeOnCompleted(ref Awaiter, ref StateMachine);
public AsyncTask Task => _MB.Task as AsyncTask;
}
[AsyncMethodBuilder(typeof(ATaskMB<>))]
public class AsyncTask<T> : Task<T> {
public AsyncTask( Func<T> Ak = null ) : base(Ak ?? (() => throw new NotSupportedException())) { }
public async Task<T> InvokeAsync() => await ConfigureAwait(false);
public void Invoke(bool Block = false) {
if (Block) {
ThreadHelper.JoinableTaskFactory.Run(async () => await InvokeAsync());
} else {
ThreadHelper.JoinableTaskFactory.RunAsync(async () => await InvokeAsync());
}
}
[SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "Operator Name")]
public static implicit operator AsyncTask<T>(Func<Task> Lambda) => (AsyncTask<T>)Lambda.Invoke();
public static implicit operator Func<Task>(AsyncTask<T> ATask) => async () => await ATask.InvokeAsync().ConfigureAwait(false);
[SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "<Pending>")]
public static AsyncTask<T> GetAsyncTask( IAsyncOperation<T> Op ) => (AsyncTask<T>)Op.AsTask();
}
}