-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializableTask.cs
More file actions
69 lines (63 loc) · 2.9 KB
/
InitializableTask.cs
File metadata and controls
69 lines (63 loc) · 2.9 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
using Hi3Helper.Plugin.Core.Utility;
using Microsoft.Extensions.Logging;
using System;
using System.Runtime.InteropServices.Marshalling;
using System.Threading;
using System.Threading.Tasks;
using static Hi3Helper.Plugin.Core.SharedStatic;
// ReSharper disable IdentifierTypo
// ReSharper disable StringLiteralTypo
namespace Hi3Helper.Plugin.Core;
/// <summary>
/// The base class where its derived class must require asynchronous initialization before use.
/// </summary>
[GeneratedComClass]
public abstract partial class InitializableTask : IInitializableTask
{
public virtual void InitAsync(in Guid cancelToken, out nint result)
{
CancellationTokenSource tokenSource = ComCancellationTokenVault.RegisterToken(in cancelToken);
CancellationToken token = tokenSource.Token;
result = InitAsync(token).AsResult();
}
/// <summary>
/// Perform asynchronous initialization of the instance.
/// </summary>
/// <param name="token">A <see cref="CancellationToken"/> that can be used to cancel the initialization process.</param>
/// <returns>
/// A <see cref="Task{Int32}"/> representing the asynchronous operation.
/// <para>
/// Returns <c>69420</c> if the initialization completes successfully after a 10-second delay.
/// Returns <see cref="int.MinValue"/> if the operation is canceled via the provided token.
/// </para>
/// </returns>
/// <remarks>
/// Note for Plugin Developers:<br/>
/// This base implementation is intended as a placeholder and should be overridden in derived classes.
/// It demonstrates async interop and cancellation support for plugin initialization.
/// </remarks>
protected virtual async Task<int> InitAsync(CancellationToken token)
{
InstanceLogger.LogDebug("Hello World! from Initializable->InitAsync()!");
InstanceLogger.LogDebug("If you see this message, that means you need to override this method with your own Initializable member class.");
InstanceLogger.LogDebug("");
InstanceLogger.LogDebug("Moreover, this is a test method to ensure that the interop async is working as expected.");
InstanceLogger.LogDebug("Delaying for 10 seconds and you should've expected to get a return value of: 69420");
InstanceLogger.LogDebug("You can also try to cancel this method by passing the Guid Cancel token to IPlugin.CancelAsync() method.");
try
{
await Task.Delay(10000, token);
InstanceLogger.LogDebug("Delay is done! Exiting method and returning: 69420...");
return 69420;
}
catch (OperationCanceledException)
{
InstanceLogger.LogError("Delay is cancelled! Exiting method and returning: {Value}...", int.MinValue);
return int.MinValue;
}
}
/// <summary>
/// Free the resources associated by an instance.
/// </summary>
public abstract void Free();
}