public bool Request(S service, int timeout, out R response) { lock (_lock) { Ticket myTicket; if (CheckForAcceptingTokens(out myTicket, service)) { SyncUtils.Notify(_lock, myTicket._condition); } else { myTicket.token = new Token(service); myTicket._condition = new Object(); _services.AddLast(myTicket); } int lastTime = (timeout != Timeout.Infinite) ? Environment.TickCount : 0; while (true) { try { SyncUtils.Wait(_lock, myTicket._condition, timeout); if (myTicket.processing == Status.Concluded) { response = myTicket.token.response; return(true); } if (SyncUtils.AdjustTimeout(ref lastTime, ref timeout) == 0 && myTicket.processing == Status.Open) { _services.Remove(myTicket); response = default(R); return(false); } } catch (ThreadInterruptedException e) { if (myTicket.processing == Status.Open) { Thread.CurrentThread.Interrupt(); _services.Remove(myTicket); response = default(R); return(false); } if (myTicket.processing == Status.Concluded) { response = myTicket.token.response; return(true); } timeout = Timeout.Infinite; } } } }
private void FindTokenInProcessingRequests(ref Token myToken, R response) { foreach (Ticket ticket in _processingRequests) { if (ticket.token.Equals(myToken)) { myToken.response = response; ticket.processing = Status.Concluded; _processingRequests.Remove(ticket); SyncUtils.Notify(_lock, ticket._condition); return; } } }