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

时间:2013-12-12 17:35:07

标签: .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)

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

1 个答案:

答案 0 :(得分:8)

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

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

修复信号量名称。