Sometimes (though rarely) I get a System.AccessViolationException
from the following line of code
var background = deviceWatcher.GetBackgroundTrigger(new List<DeviceWatcherEventKind> { DeviceWatcherEventKind.Add, DeviceWatcherEventKind.Remove, DeviceWatcherEventKind.Update });
System.AccessViolationException
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
What should I be doing differently to prevent this exception?
Since your issue is not 100% reproduce, we cannot reproduce your issue on our side. So that this should not be the code issue.
Actualy this issue shouldn't happen in managed code. You can try setting Visual Studio Debugger to bypass this exception:
Tools menu ->Options -> Debugging -> General -> Uncheck this option "Suppress JIT optimization on module load"
More details you could reference this similar thread.
User contributions licensed under CC BY-SA 3.0