I want to build a stack of resources that will be used by different threads, but want to block to calling thread up to a timeout until a resource becomes available. The method WaitUntilTheStackHasMember() is the part I am lacking. I though of using a method like the one described on MSDN's timer and autoreset event, but it got complicated.
Is there an easier way to do this?
Class ResStack
{
public TimeSpan TimeOut { get; set; }
private object lockSync;
private Stack<Resource> Resources;
ResStak()
{
// populate Stack
}
public void AddResource (Resource resource)
{
lock (lockSync)
{
Resource.Push(resource);
}
}
private Resource PopRes()
{
Resource res = null;
lock (lockSync)
{
if (Resources.Count > 0)
{
res = Resources.Pop();
}
else
{
WaitUntilTheStackHasMember() // Not implemented
}
}
return res;
}