I have a lock to synchronize access to Bluetooth resources which I am using in 10 different places and I want to keep track of when was the last time this lock was acquired without adding a piece of code to all the 10 places where it's being used.
Consider
private DateTime lastTimeLockAcquired;
private static object _lock = new Object()
Which is being used in different places like
public SendData1(){
lock(_lock){
// Do work
}
public sendData2(){
lock(_lock){
// Do work
}
My initial idea was to create functions like
private void GetLock(){
Monitor.Enter(_lock);
lastTimeLockAcquired = DateTime.Now;
}
private void ReleaseLock(){
Monitor.Exit(_lock);
}
But I would prefer to create a class and move the whole lock and DateTime object to a separate place and access the lock from there if there is any clean way to do that.