I'm developing a thread safe class and I want to wrap async method call around a sync method call. Maybe I'm not looking for a definition of "asynccall" method, but something close to it. When calling AsyncToStartSearchPage() on the instance, I want the calling thread to block until the new thread obtains the lock on the object. That's where the semaphore comes in. The asyncCalls lock is to restrict the use. Maybe it isn't the best implementation, but I feel it needs some way of restricting the number of async requests at a time. Any feed back would be great. The application use is that I have a consumer/producer model where a thread calling this method would begin the ToStartPage() method call and place it into the buffer. On the other side I want a thread to take the object with no possibilty of changing the state of the object before ToStartPage() has finished. Before this, I had issues where the consumer would obtain lock on the object before the spun of thread was able to obtain lock and perform the ToStartPage().
private readonly Object obj_Lock = new Object();
private readonly Object asyncCalls = new Object();
private Semaphore asyncSema = new Semaphore(0, 1);
//sync method call which has a async calling wrapper
public void ToSearchPage()
{
lock (obj_Lock)
{
//do something
}
}
//public async method wrapper
public bool AsyncToStartSearchPage()
{
//used to limit one async call.
//not crazy about this part, but I feel it needs something like this
if (Monitor.TryEnter(asyncCalls, 1))
{
try
{
Thread t = new Thread(asyncToStartPage);
t.Start();
// blocks until the new thread obtains lock on object
asyncSema.WaitOne();
}
finally
{
Monitor.Exit(asyncCalls);
}
return true;
}
else
{
return false;
}
}
private void asyncToStartPage()
{
lock (obj_Lock)
{
// now that I have aquired lock, release calling thread
asyncSema.Release();
ToSearchPage();
}
}