所以我收到这个错误,看起来好像是一个损坏的垃圾收集:
应用程序因“.NET 运行时内部错误”而崩溃 https://stackoverflow.com/questions/4367664/application-crashes-with-internal-error-in-the-net-runtime
完整的错误是:
由于 IP 71C571C8 (71B20000) 处的 .NET 运行时出现内部错误,该进程被终止,退出代码为 80131506。
它运行于:
框架版本:v4.0.30319
重复运行此函数时会出现不一致的情况:
public static int GetMdiTitledChildWindows(IntPtr parentWindow)
{
IntPtr mdiClient = FindWindowEx(parentWindow, IntPtr.Zero, MdiClient, "");
List<IntPtr> handles = new List<IntPtr>();
EnumChildWindows(mdiClient, (hwnd, param) =>
{
handles.Add(hwnd);
return true;
}, IntPtr.Zero);
int counter = 0;
foreach (IntPtr handle in handles)
{
StringBuilder builder = new StringBuilder();
GetWindowText(handle, builder, GetWindowTextLength(handle)+1);
if (builder.Length > 0)
{
counter++;
}
}
return counter;
}
Where FindWindowEx()
, EnumChildWindows()
and GetWindowText()
所有 p/invoke 签名的定义都与此类似:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
该错误似乎仅在我多次运行该方法后才会发生,但是,这种情况并不会始终发生。有时有效,有时无效。
对于如何解决这个问题,有任何的建议吗?