com.shinolab.autd3-sdk 0.10.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AsyncOps.cs CHANGED
@@ -1,5 +1,6 @@
1
1
  using System;
2
2
  using System.Runtime.InteropServices;
3
+ using System.Threading;
3
4
  using System.Threading.Tasks;
4
5
 
5
6
  namespace AUTD3
@@ -26,6 +27,33 @@ namespace AUTD3
26
27
  return tcs.Task;
27
28
  }
28
29
 
30
+ internal static async Task<IntPtr> InvokeAsync(Action<IntPtr, CompletionCallback, IntPtr> call, CancellationToken cancellationToken)
31
+ {
32
+ if (!cancellationToken.CanBeCanceled)
33
+ {
34
+ return await InvokeAsync((cb, ud) => call(IntPtr.Zero, cb, ud)).ConfigureAwait(false);
35
+ }
36
+ var token = NativeClient.autd3_cancel_token_new();
37
+ try
38
+ {
39
+ using (cancellationToken.Register(() => NativeClient.autd3_cancel_token_cancel(token)))
40
+ {
41
+ try
42
+ {
43
+ return await InvokeAsync((cb, ud) => call(token, cb, ud)).ConfigureAwait(false);
44
+ }
45
+ catch (Autd3Exception e) when (e.Code == Autd3ErrorCode.Aborted && cancellationToken.IsCancellationRequested)
46
+ {
47
+ throw new OperationCanceledException(e.Message, e, cancellationToken);
48
+ }
49
+ }
50
+ }
51
+ finally
52
+ {
53
+ NativeClient.autd3_cancel_token_free(token);
54
+ }
55
+ }
56
+
29
57
  [MonoPInvokeCallback(typeof(CompletionCallback))]
30
58
  private static void OnComplete(int code, IntPtr value, IntPtr msg, IntPtr userData)
31
59
  {