Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Creating a Semaphore fails with IoException on Win 2008

The following code is failing for me on Windows 2008. It succeeds in Win7.

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

I get the following error:

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)

I've searched around and can't find an explanation. Does anyone have any ideas?

like image 936
Charles Sullivan Avatar asked Dec 12 '13 17:12

Charles Sullivan


1 Answers

You are the victim of a bug in the error reporting. The exception message is strange because the WinIOError() method it uses is shared with the SerialPort class. When the Windows error code is 2 (File not found) or 3 (Path not found) then you get the exception message that's only appropriate for a serial port. Thus the weirdo "The specified port does not exist" message.

I am a victim of your code snippet, you obfuscated the real problem. The semaphore name you used isn't actually "my-test-semaphore". I can't guess what it really looks like but it violates the rules for a properly named Windows object. It might look like "backslashes\\are not okay".

Fix the semaphore name.

like image 115
Hans Passant Avatar answered Oct 16 '22 21:10

Hans Passant