I have a Visual Studio 2008 C# .NET 3.5 project where a class listens for an event invocation from another class that is multithreaded. I need to ensure that my event only allows simultaneous access to a maximum of 10 threads. The 11th thread should block until one of the 10 finishes.
myobj.SomeEvent += OnSomeEvent;
private void OnSomeEvent(object sender, MyEventArgs args)
{
// allow up to 10 threads simultaneous access. Block the 11th thread.
using (SomeThreadLock lock = new SomeThreadLock(10))
{
DoUsefulThings(args.foo);
}
}
I do not have control over the other MyObj
class, so I cannot implement a threadpool there.
What is the best way to implement this?
Thanks, PaulH
finally
block of a try/catch. This is important stuff- in case your code throws an exception, you want to make sure the semaphore is exited. – Stannary