Powershell中使用回调的异步命名管道 [英] Asynchronous named pipes in powershell using callbacks

查看:143
本文介绍了Powershell中使用回调的异步命名管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Powershell中的回调通过.net NamedPipeServerStream异步使用命名管道.

I'm attempting to use a named pipe using a .net NamedPipeServerStream asynchronously using callbacks in powershell.

我当前正在使用以下代码:

I'm currently using the following code:

服务器端:

$myCallback = [AsyncCallback]{
  "Connected"
}

$pipe = New-Object System.IO.Pipes.NamedPipeServerStream("alert", [System.IO.Pipes.PipeDirection]::InOut, 1, [System.IO.Pipes.PipeTransmissionMode]::Message, [System.IO.Pipes.PipeOptions]::Asynchronous)
$pipe.BeginWaitForConnection($myCallback, "alertCallback")

客户端:

$pipe = new-object System.IO.Pipes.NamedPipeClientStream("alert");
$pipe.Connect(3000); 

$sw = new-object System.IO.StreamWriter($pipe);
$sw.WriteLine("Test"); 

我首先调用服务器端代码,该代码报告回调已成功注册

I call the server side code first, which reports that the callback has been registered successfully

AsyncState                                        IsCompleted AsyncWaitHandle                       CompletedSynchronously
----------                                        ----------- ---------------                       ----------------------
alertCallback                                           False System.Threading.ManualRese...                         False

客户端代码一旦被调用,PowerShell服务器脚本就会崩溃-不会引发异常,我只是得到一个"Powershell已停止工作"的Windows样式错误框.我不知道为什么会发生这种情况,而且似乎无法从脚本中获取任何异常或其他调试信息来了解出了什么问题.如果我尝试同步执行相同的操作,那么一切都会按预期进行.非常感谢您的帮助.

As soon as the client side code is called the powershell server script crashes - an exception is not thrown, simply I get a "Powershell has stopped working" windows style error box. I'm at a loss as to why this occurs, and I can't seem to get any exception or other debugging information out of the script to understand what is going wrong. If I attempt to do the same synchronously everything works as expected. Any help is much appreciated.

推荐答案

  1. 希望客户端代码在工作中或使用不同的脚本.
  2. pipeDirection 添加到客户端管道.
  3. 在写入客户端流之前添加 sw.AutoFlush = $ true .
  4. 如果仍然无法正常运行,请尝试在消息中添加CRLF: $ sw.WriteLine("Test \ r \ n");
  1. Hope Client code is inside job or in different script.
  2. Add pipeDirection to Client pipe.
  3. Add sw.AutoFlush = $true before writing to client stream.
  4. If it still does not work, try adding CRLF to message: $sw.WriteLine("Test\r\n");

希望这会有所帮助.

这篇关于Powershell中使用回调的异步命名管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆