问题描述
我在桌面应用程序中做了一些非常简单的错误记录,该应用程序通过 SerialPort
与设备对话。我要做的一件事是设置一个全局异常捕获器,该捕获器只使用以下命令记录堆栈跟踪:
I do some very simple error logging in my desktop application which speaks to equipment over a SerialPort
. One thing I do is setup a global exception catcher which does nothing but log the stack trace using:
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
我有一个用户的日志中出现错误。没有人遇到这个问题。我不知道在哪里可以找到这个未捕获的异常。而且我不知道该在哪里尝试捕获它,因为我不完全了解它是什么。
I have one user who is having an error that is showing up in his logs. No one else is having the issue. I have no idea where to search for this uncaught exception. And I have no idea where I should try to catch it because I don't fully understand what it is.
我使用.NET SerialPort
对象和我在打开端口时所做的所有操作,并执行一些简单的读写操作。
I use the .NET SerialPort
object and all I do in open the port and do some simple reads and writes.
有人能解释此异常是什么/意味着,我可以做些什么来解决它,或者应该去尝试发现错误?每次触摸 SerialPort
对象时,我都会在try和catch块中这样做。因此,我完全不知所措。
Is anyone able to explain what this exception is/means, what I can do to fix it, or where I should look to try and catch the error? Every time I touch my SerialPort
object I am doing so within try and catch blocks. So I am at a complete loss.
System.ObjectDisposedException: Safe handle has been closed
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(SafeFileHandle hFile, NativeOverlapped* lpOverlapped, Int32& lpNumberOfBytesTransferred, Boolean bWait)
at System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
推荐答案
被扔在另一个线程上。将try-catch块移至执行读/写操作的线程中,然后查看是否可以解决您的问题。
It appears the exception is being thrown on a different thread. Move your try-catch block into the thread that does the read/writing and see if that fixes your problem.
另请参见使用任务的问题。
Also see this question for using Task.
这篇关于我认为我的SerialPort负责此ObjectDisposedException,但我不知道它在哪里发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!