This works:
int _counter;
readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
async void Button_Click(object sender, RoutedEventArgs e)
{
if (_semaphore.Wait(0))
{
Title = $"{ ++_counter}";
await Task.Delay(1000); // simulate work
Title = $"{ --_counter}";
_semaphore.Release();
}
}
After first click further buttons clicks are ignored until work is finished. Tittle
can be 1
or 0
.
And this doesn't work
void Button_Click(object sender, RoutedEventArgs e)
{
if (_semaphore.Wait(0))
{
Test(); // moving code into separate method
_semaphore.Release();
}
}
async void Test()
{
Title = $"{ ++_counter}";
await Task.Delay(1000); // simulate work
Title = $"{ --_counter}";
}
Clicking button continuously will rise Tittle
to 2
, 3
, and so on.
What am I doing wrong?