我有以下用于实例化Java应用程序的类:
private AccessBridgeFunctions _javaFunctions;
private AccessBridge _javaBridge;
private readonly ConcurrentDictionary<IntPtr, AccessibleWindow> _openWindows;
private List<AccessibleJvm> _javaApplications;
internal JavaApplication()
{
_javaBridge = Factory.CreateAccessBridge();
_javaBridge.Initialize();
Thread.Sleep(10000); //added time here as I thought it would solve the issue
Task task = Task.Factory.StartNew(() => GetJavaWindows());
while (task.Status != TaskStatus.RanToCompletion)
{
Application.DoEvents();
Thread.Sleep(500);
}
_javaApplications = GetJavaWindows();
_javaFunctions = _javaBridge.Functions;
}
private List<AccessibleJvm> GetJavaWindows()
{
return _javaBridge.EnumJvms(hwnd => GetOrAddCache(_javaBridge, hwnd));
}
private AccessibleWindow GetOrAddCache(AccessBridge accessBridge, IntPtr hwnd)
{
return _openWindows.GetOrAdd(hwnd, key => accessBridge.CreateAccessibleWindow(key));
}
此类的作用是获取我PC上所有正在运行的程序,如果正在运行的程序是Java程序,则创建一个AccessibleWindow对象。
问题有时是,我在此方法中遇到 SystemViolationException :
private AccessibleWindow GetOrAddCache(AccessBridge accessBridge, IntPtr hwnd)
{
return _openWindows.GetOrAdd(hwnd, key => accessBridge.CreateAccessibleWindow(key));
}
特别是:
accessBridge.CreateAccessibleWindow(key);
我没有该异常的屏幕截图,因为该异常不会一直发生,但是它表示我正在读取损坏的内存。
是否存在导致此问题发生的具体原因,并且有什么方法可以防止此问题发生?我不确定这是否是罪魁祸首,但是请注意,我的计算机当前以64位运行时,我使用的是32位WindowsAccessBridge.dll。