generated from nventive/Template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSkipWhileExecutingCommandStrategy.cs
51 lines (47 loc) · 1.35 KB
/
SkipWhileExecutingCommandStrategy.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Chinook.DynamicMvvm;
namespace Chinook.DynamicMvvm
{
public static partial class DynamicCommandStrategyExtensions
{
/// <summary>
/// Will skip executions if the command is already executing.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns><see cref="IDynamicCommandBuilder"/></returns>
public static IDynamicCommandBuilder SkipWhileExecuting(this IDynamicCommandBuilder builder)
=> builder.WithStrategy(new SkipWhileExecutingCommandStrategy());
}
/// <summary>
/// This <see cref="DelegatingCommandStrategy"/> will skip executions if the command is already executing.
/// </summary>
public class SkipWhileExecutingCommandStrategy : DelegatingCommandStrategy
{
public int _isExecuting;
/// <summary>
/// Initializes a new instance of the <see cref="SkipWhileExecutingCommandStrategy"/> class.
/// </summary>
public SkipWhileExecutingCommandStrategy()
{
}
/// <inheritdoc />
public override async Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
{
if (Interlocked.CompareExchange(ref _isExecuting, 1, 0) == 0)
{
try
{
await base.Execute(ct, parameter, command);
}
finally
{
_isExecuting = 0;
}
}
}
}
}