.NET 创建信号量失败并在 Win 2008 上出现 IoException

标签 .net semaphore ioexception

以下代码在 Windows 2008 上对我来说失败了。它在 Win7 中成功。

return new Semaphore(1, 1, "my-test-semaphore");

我收到以下错误:
System.IO.IOException: The specified port does not exist.
  at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
  at System.IO.Ports.InternalResources.WinIOError()
  at System.Threading.Semaphore..ctor(Int32 initialCount, Int32 maximumCount, String name)
  at Throttle.Program.CreateSemaphore(String passthroughApplication)
  at Throttle.Program.Main(String[] args)

我四处搜索,找不到解释。有没有人有任何想法?

最佳答案

您是错误报告中错误的受害者。异常消息很奇怪,因为它使用的 WinIOError() 方法与 SerialPort 类共享。当 Windows 错误代码为 2(未找到文件)或 3(未找到路径)时,您会收到仅适用于串行端口的异常消息。因此,奇怪的“指定的端口不存在”消息。

我是你代码片段的受害者,你混淆了真正的问题。您使用的信号量名称实际上并不是“my-test-semaphore”。我无法猜测它到底是什么样子,但它违反了正确命名的 Windows 对象的规则。它可能看起来像 "backslashes\\are not okay" .

修复信号量名称。

关于.NET 创建信号量失败并在 Win 2008 上出现 IoException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20550304/

相关文章:

.net - 使用 SustainSys Saml2 扩展方法 ToHttpRequestData 编译错误 "call is ambiguous"

c# - 防止实现不是接口(interface)方法的方法

java 男女通用浴室

java - SOAP 请求 : Illegal Protocol https for HTTP URLConnection Factory

java - 运行 sbt 失败 - java.io.IOException : No space left on device

java - 写入文件 : IOException: Stream closed

.net - F#简单类型和结构比较

c# - .NET 中的编码风格 : whether to refactor into new method or not?

objective-c - 等待 dispatch_semaphore 以等待许多异步任务完成的正确方法

C 信号量同步