同じコマンドが同時に複数回実行されないように、頻繁なユーザー ジェスチャを無効にしようとしています。
private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1);
public bool MyCommandCanExecute { get; set; } = true;
public DelegateCommand MyCommand { get; set; }
MyCommand = new DelegateCommand(async () =>
{
await _semaphoreSlim.WaitAsync();
try
{
MyCommandCanExecute = false;
// run code
}
finally
{
MyCommandCanExecute = true;
_semaphoreSlim.Release();
}
}).ObservesCanExecute(p => MyCommandCanExecute);
これが正しいかどうかはわかりません。
- やり過ぎですか
SemaphoreSlim
、それとも安全のためにそのままにしておくべきですか? ObservesCanExecute()
手動でフラグのオンとオフを切り替えているので必要ですか?- 同じビュー モデルにいくつかのコマンドがあり、それぞれに個別の CanExecute フラグがあります。あるコマンドを実行中に別のコマンドを有効にする必要がある場合にすぐに遭遇すると思います。
_semaphoreSlim
進行中のすべてのコマンドを保留しますか、それとも同じコマンドの複数の呼び出しを保留しますか?