请问关于c的具体应用场景有哪些?

2026-04-29 00:202阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计303个文字,预计阅读时间需要2分钟。

请问关于c的具体应用场景有哪些?

我正在使用NamedPipeServerStream在两个进程间进行通信。这是初始化和连接管道的代码:

csharpvoid Foo(IHasData objectProvider){ Stream stream=objectProvider.GetData(); if (stream.Length > 0) { using (NamedPipeServerStream namedPipeServerStream=new NamedPipeServerStream(pipeName, PipeDirection.InOut)) { namedPipeServerStream.WaitForConnection(); // 进行通信操作 } }}

我正在使用NamedPipeServerStream在两个进程之间进行通信.这是我初始化和连接管道的代码:

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)

请问关于c的具体应用场景有哪些?

我认为在等待返回之后,一切都应该准备好了.

如果我使用pipeServer.WaitForConnection()一切正常,但如果管道没有连接则挂起应用程序不是一个选项.

你需要拨打 EndWaitForConnection.

var asyncResult = pipeServer.BeginWaitForConnection(PipeConnected, this); if (asyncResult.AsyncWaitHandle.WaitOne(5000)) { pipeServer.EndWaitForConnection(asyncResult); // ... }

见:IAsyncResult design pattern.

本文共计303个文字,预计阅读时间需要2分钟。

请问关于c的具体应用场景有哪些?

我正在使用NamedPipeServerStream在两个进程间进行通信。这是初始化和连接管道的代码:

csharpvoid Foo(IHasData objectProvider){ Stream stream=objectProvider.GetData(); if (stream.Length > 0) { using (NamedPipeServerStream namedPipeServerStream=new NamedPipeServerStream(pipeName, PipeDirection.InOut)) { namedPipeServerStream.WaitForConnection(); // 进行通信操作 } }}

我正在使用NamedPipeServerStream在两个进程之间进行通信.这是我初始化和连接管道的代码:

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)

请问关于c的具体应用场景有哪些?

我认为在等待返回之后,一切都应该准备好了.

如果我使用pipeServer.WaitForConnection()一切正常,但如果管道没有连接则挂起应用程序不是一个选项.

你需要拨打 EndWaitForConnection.

var asyncResult = pipeServer.BeginWaitForConnection(PipeConnected, this); if (asyncResult.AsyncWaitHandle.WaitOne(5000)) { pipeServer.EndWaitForConnection(asyncResult); // ... }

见:IAsyncResult design pattern.