So, I have a method, which contains async call to the server. That code is called from 3rd party tool, which somehow sometimes calls same method several times in a row from different threads, so I can't affect that.
What I want to be sure is that my method is called once, and then, another calls should be ignored.
At first, I tried to lock(locker) with bool isBusy, but that is not satisfied me, as async request was still called several times from second thread, which was fast enough to see isBusy=true;
Then, I tried Monitor
object obj = new object();
Monitor.TryEnter(obj);
try
{
var res = await _dataService.RequestServerAsync(SelectedIndex, e.StartIndex, e.Count);
****
}
finally
{
Monitor.Exit(obj);
}
However, on Exit()
, I'm getting exception:
A first chance exception of type 'System.Threading.SynchronizationLockException'
Is there any other way to guarantee only 1 time execution of the code?