-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlExtensions.cs
More file actions
51 lines (45 loc) · 1.37 KB
/
Copy pathControlExtensions.cs
File metadata and controls
51 lines (45 loc) · 1.37 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
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WebToolsDataMonitor
{
public static class ControlExtensions
{
// 🔥 FIXED: Explicitly await the inner task block before completing the completion source
public static Task<T> InvokeAsync<T>(this Control control, Func<Task<T>> code)
{
var tcs = new TaskCompletionSource<T>();
control.BeginInvoke((MethodInvoker)(async () =>
{
try
{
// Unwraps the inner Task<string> generated by WebView2
T result = await code();
tcs.SetResult(result);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
}));
return tcs.Task;
}
public static Task InvokeAsync(this Control control, Func<Task> code)
{
var tcs = new TaskCompletionSource<bool>();
control.BeginInvoke((MethodInvoker)(async () =>
{
try
{
await code();
tcs.SetResult(true);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
}));
return tcs.Task;
}
}
}