/// <summary> /// Waits for the number of elements in the buffer to reach a certain size, or until the timeout is reached or the other wait handle is signaled /// </summary> /// <param name="nSizeInBuffer"></param> /// <param name="nTimeout"></param> /// <param name="otherhandletowaiton"></param> /// <returns></returns> public bool WaitForSize(int nSizeInBuffer, int nTimeout, System.Threading.WaitHandle otherhandletowaiton) { lock (BufferLock) { if (m_nSize >= nSizeInBuffer) { return(true); } SizeEvent.Reset(); } TimeSpan tsElapsed; DateTime dtStart = DateTime.Now; WaitHandle[] handles = new WaitHandle[] { SizeEvent, otherhandletowaiton }; if (otherhandletowaiton == null) { handles = new WaitHandle[] { SizeEvent } } ; do { int nWait = WaitHandle.WaitAny(handles, nTimeout); if (nWait == 1) { return(false); } else if (nWait == 0) { lock (BufferLock) { SizeEvent.Reset(); if (m_nSize >= nSizeInBuffer) { return(true); } } } else { return(false); } tsElapsed = DateTime.Now - dtStart; if (nTimeout != Timeout.Infinite) { nTimeout = (int)tsElapsed.TotalMilliseconds; if (nTimeout <= 0) { break; } } }while (true); return(false); } AutoResetEvent SizeEvent = new AutoResetEvent(false); object BufferLock = new object();
//////////////////////////////////////////////////////////// /// <summary> /// Construct the size arguments from a size event /// </summary> /// <param name="e">Size event</param> //////////////////////////////////////////////////////////// public SizeEventArgs(SizeEvent e) { Width = e.Width; Height = e.Height; }