I delete and create the corresponding closed device variable and try to connect this device via the COM port again using the Open() function when the device itself is turned off, respectively (attempts are made many times until the device is connected).

And I get an error with this trace:

in System.Runtime.InteropServices.SafeHandle.DangerousAddRef (Boolean & success)
in System.StubHelpers.StubHelpers.SafeHandleAddRef (SafeHandle pHandle, Boolean & success)
at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult (SafeFileHandle hFile, NativeOverlapped * lpOverlapped, Int32 & lpNumberOfBytesTransferred, Boolean bWait)
in System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent ()
in System.Threading.ThreadHelper.ThreadStart_Context (Object state)
in System.Threading.ExecutionContext.RunInternal (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart ()

The fact is that I have already tried to run it and run it using Task.Factory.StartNew(() => ... and nothing happens anyway.

The exception crashes as systematically every other time.

Here, the port name is specified from the program's default settings, the COM port itself does not close, but the variable that works with it is re-created, the device itself is not connected to the port and every time (and there are many) the Open () function does not work, but the program is all flies equally, directly reaching only the Open () function. I could not send the library of work with the device here:

 private void billAcceptorConnect() { try { billAcceptor = new CashCodeSm(); billAcceptor.PortName = Properties.Settings.Default.ComPort; billAcceptor.Open(); billAcceptor.IsAcceptanceEnabled = false; billAcceptor.BillsOnEscrow += billAcceptor_BillsOnEscrow; billAcceptor.BillsStacked += billAcceptor_BillsStacked; billAcceptor.CashBoxRemoved += billAcceptor_CashBoxRemoved; billAcceptor.BillsRejected += billAcceptor_BillsRejected; billAcceptor.BillsReturned += billAcceptor_BillsReturned; billAcceptor.CashBoxAttached += billAcceptor_CashBoxAttached; billAcceptor.CashBoxIsAlmostFull += billAcceptor_CashBoxIsAlmostFull; billAcceptor.CashBoxIsFull += billAcceptor_CashBoxIsFull; billAcceptor.Error += billAceptor_Error; } catch { billAcceptor = null; } } 

Closed due to the fact that it was off-topic by Athari , zRrr , user194374, Nick Volynkin 9 Jun '16 at 6:19 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Athari, zRrr, Community Spirit, Nick Volynkin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Uh ... Error loading mscorlib ? Very strange, without mscorlib, the program should not run at all. - VladD
  • Try to create a reproducing minimal example. Without it, it is not clear what could be the problem. - VladD

0