-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncRelayCommand.cs
More file actions
97 lines (87 loc) · 3.25 KB
/
Copy pathAsyncRelayCommand.cs
File metadata and controls
97 lines (87 loc) · 3.25 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Dreamine.MVVM.ViewModels
{
/// <summary>
/// Provides an asynchronous <see cref="ICommand"/> implementation that prevents
/// re-entrant execution and exposes failure information.
/// </summary>
public sealed class AsyncRelayCommand : ICommand
{
private readonly Func<Task> _execute;
private readonly Func<bool>? _canExecute;
// Interlocked: 0 = idle, 1 = executing. Prevents concurrent entry without lock.
private int _isExecuting;
private volatile Exception? _lastException;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncRelayCommand"/> class.
/// </summary>
/// <param name="execute">The asynchronous execute delegate.</param>
/// <param name="canExecute">The optional can-execute delegate.</param>
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
/// <inheritdoc />
public event EventHandler? CanExecuteChanged;
/// <summary>
/// Occurs when the asynchronous delegate throws an exception.
/// </summary>
public event EventHandler<Exception>? ExecutionFailed;
/// <summary>
/// Gets the last exception thrown by the asynchronous delegate, or <c>null</c>.
/// </summary>
public Exception? LastException => _lastException;
/// <inheritdoc />
public bool CanExecute(object? parameter)
{
return Volatile.Read(ref _isExecuting) == 0 && (_canExecute?.Invoke() ?? true);
}
/// <inheritdoc />
public async void Execute(object? parameter)
{
// Atomically claim execution slot; bail if already executing.
if (Interlocked.CompareExchange(ref _isExecuting, 1, 0) != 0)
{
return;
}
RaiseCanExecuteChanged();
try
{
await _execute().ConfigureAwait(true);
}
catch (Exception ex)
{
_lastException = ex;
var handler = ExecutionFailed;
if (handler is not null)
{
handler.Invoke(this, ex);
}
else
{
// No subscriber — emit a diagnostic trace so the exception is visible
// in the Output window rather than silently disappearing.
Debug.WriteLine(
$"[AsyncRelayCommand] Unhandled exception (subscribe to ExecutionFailed to suppress this): {ex}");
}
}
finally
{
Interlocked.Exchange(ref _isExecuting, 0);
RaiseCanExecuteChanged();
}
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged"/> event.
/// </summary>
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
}