private void RemoveMessage(object state, bool timedOut) { MessageIO io = (MessageIO)state; io.Timeout = timedOut; io.AutoReset.Set(); }
public void BeginMessage(Guid messageID, int timeout) { AutoResetEvent autoReset = new AutoResetEvent(false); Monitor.Enter(Messages); Messages.Add(messageID, autoReset); Monitor.Exit(Messages); MessageIO io = new MessageIO(); io.ID = messageID; io.AutoReset = autoReset; io.Timeout = false; var data = Recycle.GetData(messageID); if (data == null) { ThreadPool.RegisterWaitForSingleObject(autoReset, RemoveMessage, io, timeout, true); autoReset.WaitOne(); } else { IOs.Add(messageID, data); } Monitor.Enter(Messages); Messages.Remove(messageID); Monitor.Exit(Messages); if (io.Timeout) { throw new TimeoutException("连接超时。"); } }