Search code examples
c#functionlockingblocking

Blocking a function call in C#


How do I block a function call in C#?

This function gets repeatedly called by different classes.I want to lock it up so that no one else can use it until I perform my current operation. Then,i want to release it again.

Please help..

Thanks

Edit:I'm not actually using threads...but I am using timers that call the function repeatedly and it's also called among different classes.


Solution

  • Use a lock:

    private static object lockObject = new object();
    
    lock(lockObject) {
      // ... code goes here...
    }