以下代码在 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/