generated from nventive/Template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLockCommandStrategy.cs
49 lines (44 loc) · 1.21 KB
/
LockCommandStrategy.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Chinook.DynamicMvvm
{
public static partial class DynamicCommandStrategyExtensions
{
/// <summary>
/// Will lock the command execution.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns><see cref="IDynamicCommandBuilder"/></returns>
public static IDynamicCommandBuilder Locked(this IDynamicCommandBuilder builder)
=> builder.WithStrategy(new LockCommandStrategy());
}
/// <summary>
/// This <see cref="DelegatingCommandStrategy"/> will lock the command execution.
/// </summary>
public class LockCommandStrategy : DelegatingCommandStrategy
{
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
/// <summary>
/// Initializes a new instance of the <see cref="LockCommandStrategy"/> class.
/// </summary>
public LockCommandStrategy()
{
}
/// <inheritdoc />
public override async Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
{
try
{
await _semaphore.WaitAsync(ct);
await base.Execute(ct, parameter, command);
}
finally
{
_semaphore.Release();
}
}
}
}