public static void WaitForConnection(this NamedPipeServerStream stream, ManualResetEvent cancelEvent) { Exception exception = null; var connectEvent = new AutoResetEvent(false); stream.BeginWaitForConnection(result => { try { stream.EndWaitForConnection(result); } catch (Exception ex) { exception = ex; } connectEvent.Set(); }, null); if (WaitHandle.WaitAny(new WaitHandle[] { connectEvent, cancelEvent }) == 1) stream.Close(); if (exception != null) throw exception; }
/// <summary> /// Extends BeginWaitForConnection so that when a state object is not needed, null does not need to be passed. /// <example> /// namedpipeserverstream.BeginWaitForConnection(callback); /// </example> /// </summary> public static IAsyncResult BeginWaitForConnection(this NamedPipeServerStream namedpipeserverstream, AsyncCallback callback) { if(namedpipeserverstream == null) throw new ArgumentNullException("namedpipeserverstream"); return namedpipeserverstream.BeginWaitForConnection(callback, null); }