- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我是第一次尝试使用命名管道。在 MS 文档中找到 here ,它指出:
EndWaitForConnection must be called exactly once for every call to BeginWaitForConnection.
所以我想成为一名优秀的小程序员并遵循文档,但是 EndWaitForConnection()
在我使用它时会无限期地挂起。
所以我将我的代码精简到最低限度,看看我是否可以隔离问题但没有骰子。我从我编写的类(class)中提取了以下代码。我对其进行了修改,使其开始等待管道连接,然后立即尝试停止等待该管道连接:
private void WaitForConnectionCallBack(IAsyncResult result)
{
}
public void Start()
{
var tempPipe = new NamedPipeServerStream("TempPipe",
PipeDirection.In,
254,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous);
IAsyncResult result = tempPipe.BeginWaitForConnection(
new AsyncCallback(WaitForConnectionCallBack), this);
tempPipe.EndWaitForConnection(result); // <----- Hangs on this line right here
}
1) 为什么它卡在 EndWaitForConnection()
上?如果我想在接收到连接之前关闭我的服务器,我该如何取消这个 BeginWaitForConnection()
回调?
2) 假设我没有遇到上述问题。如果 2 个客户端尝试快速连接到我的命名管道会怎样?
我是否为它们中的每一个都获得回调调用,或者我是否必须等待接收第一个连接通知然后快速调用 EndWaitForConnection()
然后 WaitForConnectionCallBack()
再次开始监听下一个客户?
后者对我来说似乎是一种竞争条件,因为我可能没有足够快地设置连接监听器。
最佳答案
因此,适用于我的解决方案的基本框架如下:
private void WaitForConnectionCallBack(IAsyncResult result)
{
try
{
PipeServer.EndWaitForConnection(result);
/// ...
/// Some arbitrary code
/// ...
}
catch
{
// If the pipe is closed before a client ever connects,
// EndWaitForConnection() will throw an exception.
// If we are in here that is probably the case so just return.
return;
}
}
这是服务器代码。
public void Start()
{
var server= new NamedPipeServerStream("TempPipe",
PipeDirection.In,
254,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous);
// If nothing ever connects, the callback will never be called.
server.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), this);
// ... arbitrary code
// EndWaitForConnection() was not the right answer here, it would just wait indefinitely
// if you called it. As Hans Passant mention, its meant to be used in the callback.
// Which it now is. Instead, we are going to close the pipe. This will trigger
// the callback to get called.
// However, the EndWaitForConnection() that will excecute in the callback will fail
// with an exception since the pipe is closed by time it gets invoked,
// thus you must capture it with a try/catch
server.Close(); // <--- effectively closes our pipe and gets our
// BeginWaitForConnection() moving, even though any future
// operations on the pipe will fail.
}
关于c# - NamedPipeServerStream.EndWaitForConnection() 在使用时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9145438/
我有一个应用程序,当通过 eclipse 运行时,它会导致 eclipse 本身挂起。如果我将此应用程序导出为 jar 并运行它,它工作正常。但是,如果我运行(或调试)它,应用程序将显示为启动(根据
我正在将项目从 Rails2 切换到 Rails3。我跑: rails server 服务器启动没有错误: => Booting WEBrick => Rails 3.0.7 application
当我尝试使用 XCode 打开特定项目时,它挂起。当它挂起时,它显示以下屏幕: 其他项目可以正常打开,虽然挂起的项目也打开了,意味着我什么也做不了。我的 CPU 全速运行(风扇开始运转),我必须退出多
我正在使用 BNHtmlPdfKit 将 Html 呈现为 PDF。它工作得很好,但在 iOS8 中它只是挂起 [renderer drawPageAtIndex:i inRect:renderer.
我一直在尝试在 eclipse 中创建一个项目,并且有一个名为 InitRemoteEditJob 的工作正在阻止一切。它甚至没有被取消。 请建议怎么办? 最佳答案 这个错误有很多原因。 你可以试试这
我使用这个函数来发出 cURL 请求: function curl_request($options) //single custom cURL request. { $ch = curl_i
当我尝试归档某个项目时,Xcode 无法响应。如果让他一个人呆着,他会在很长一段时间后设法打开管理器。文件在那里。如果从 library/developer/xcode/archives 中手动删除,
有时我的 Eclipse 挂起,我需要猛烈地杀死它。但是,我一直无法正确地做到这一点。似乎 kill -9 实际上并没有以正确的方式关闭它,因为我仍然可以看到挂起的窗口。什么命令序列会正确杀死我的 E
我有一个JavaFX 8应用,它有时会挂起/冻结。我觉得我已经排除了造成此问题的许多原因,但它仍在发生。 不幸的是,我无法按需复制冻结/挂起。实际上,这仅发生在(到目前为止)我同事的计算机上。它可能在
我正在尝试学习网络基础知识,并且已经从this教程构建了回显服务器。我用telnet检查了服务器,它工作正常。 现在,当我使用Internet上的许多客户端示例中的一些示例时: // Create a
我正在尝试使用 SwiftUI 实现使用 Apple 登录,但在我输入正确的密码后它挂起。 我正在使用真实用户和模拟器以及 XCode 12.0 Beta。不幸的是,我现在没有可供测试的设备。我也尝试
我包括此简单的错误处理功能来格式化错误: date_default_timezone_set('America/New_York'); // Create the error handler. fun
我正在尝试为 VisualVM 安装一些插件,但它一直卡在下面的屏幕上 - 告诉我“请等待安装程序发现插件依赖项”。我运行的是 Ubuntu 12.04。当我尝试从“可用插件”列表中安装它们时,以及当
如果堆分配/取消分配/重新分配在另一个线程中进行,DbgHelp 库的 MiniDumpWriteDump() 将挂起。这是调用堆栈:DbgHelp 暂停其他线程,然后无限期地等待这些线程获得的互斥量
我正在尝试在 Eclipse C++ 版本中安装新软件。 帮助 -> 安装新软件。当我去安装新软件时,它会挂起或需要几个小时才能移动百分比。 我读到这是 JRE7 中的一个已知错误,我假设我在安装它后
这个问题已经有答案了: process.waitFor() never returns (12 个回答) 已关闭 3 年前。 我使用以下代码运行命令: open class AppRunner {
我正在尝试为 VisualVM 安装一些插件,但它一直卡在下面的屏幕上 - 告诉我“请等待安装程序发现插件依赖项”。我正在运行 Ubuntu 12.04。当我尝试从“可用插件”列表安装它们时,以及当我
如果堆分配/取消分配/重新分配在另一个线程中进行,DbgHelp 库的 MiniDumpWriteDump() 将挂起。这是调用堆栈:DbgHelp 暂停其他线程,然后无限期地等待这些线程获得的互斥量
尝试调试竞争条件,其中我们的应用程序的轮询器线程之一永远不会返回,导致 future 的轮询器永远不会被调度。用抽象术语来说,在捕获问题时隐藏我们的业务逻辑,这就是我们的代码路径。 我们必须更新远程服
我在程序完成时遇到 Java 的 ExecutorCompletionService 问题。 我需要使用 ExecutorCompletionService 而不是 ExecutorService 因
我是一名优秀的程序员,十分优秀!