Im using undetermined DLLs which can use undetermined resources such as a COM port. Some DLL methods don't have their own timeouts, so i am forced to abort the execution thread. But if the thread is using a resource such as a COM port, and i abort the thread, my program crashes with the error “Safe handle has been closed”. I know why this happens but is there any way to catch this exception or skip it, rather than an actual crash?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Solution: Running the code in a separate AppDomain bypasses the exception and crash - thanks to Sinatr in the comments.
Code example. Before (crashed)
After: (never crash)
Inspired by https://bitlush.com/blog/executing-code-in-a-separate-application-domain-using-c-sharp. Now I just need to pass around variables.