voID Foo(IHasData objectProvIDer){ Stream stream = objectProvIDer.GetData(); if (stream.Length > 0) { using (namedPipeServerStream pipeServer = new namedPipeServerStream("VisualizerPipe",PipeDirection.Out,1,PipeTransmissionMode.Byte,PipeOptions.Asynchronous)) { string currentDirectory = Path.GetDirectoryname(Assembly.GetExecutingAssembly().Location); string uifilename = Path.Combine(currentDirectory,"VisualizerUIApplication.exe"); Process.Start(uifilename); if(pipeServer.BeginWaitForConnection(PipeConnected,this).AsyncWaitHandle.WaitOne(5000)) { while (stream.CanRead) { pipeServer.WriteByte((byte)stream.ReadByte()); } } else { throw new TimeoutException("Pipe connection to UI process timed out."); } } }}private voID PipeConnected(IAsyncResult e){}
但它似乎永远不会等待.我经常遇到以下异常:
system.invalIDOperationException:管道尚未连接.
在System.IO.Pipes.Pipestream.CheckWriteOperations()
在System.IO.Pipes.Pipestream.WriteByte(字节值)
在PeachesObjectVisualizer.Visualizer.Show(IDialogVisualizerService windowservice,IVisualizerObjectProvIDer objectProvIDer)
我认为在等待返回之后,一切都应该准备好了.
如果我使用pipeServer.WaitForConnection()一切正常,但如果管道没有连接则挂起应用程序不是一个选项.
解决方法 你需要拨打 EndWaitForConnection.var asyncResult = pipeServer.BeginWaitForConnection(PipeConnected,this);if (asyncResult.AsyncWaitHandle.WaitOne(5000)){ pipeServer.EnDWaitForConnection(asyncResult); // ...}
见:IAsyncResult design pattern.
总结以上是内存溢出为你收集整理的c# – 我的NamedPipeServerStream为什么不等?全部内容,希望文章能够帮你解决c# – 我的NamedPipeServerStream为什么不等?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)