我们在Winform应用程序中看到此错误.任何人都可以帮助您看到此错误的原因,更重要的是如何修复它或避免它发生.
System.ComponentModel.Win32Exception: Error creating window handle. at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp) at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e) at System.Windows.Forms.ButtonBase.OnVisibleChanged(EventArgs e)
我的程序是一个CRM,我使用Rad Ribbon Bar,所以有许多带图像的按钮,RadGridView(有些列包含图像)和许多其他包含图像的控件.这是一个mdi父/子计划.
在很多情况下,在加载mdi子项或使用某些网格视图时,程序将挂起并给我这个错误:
OutOfMemoryException occurred in System.Drawing.dll
Run Code Online (Sandbox Code Playgroud)
我试过GC.Collect()某些部分但没有成功.设置图像没有代码!例如,为按钮设置图像我在visual studio中使用了它的属性.我在可视模式下使用属性面板以这种方式设置了所有其他控制图像.
这些是与绘图相关的一些设计师代码:
btnCustomerList.Image = global::MyApp.Properties.Resources.CustomerList32;
gridViewCommandColumn1.Image = global::MyApp.Properties.Resources.ViewShop32;
Run Code Online (Sandbox Code Playgroud)
当使用应用程序一段时间后出现错误时,它将出现Program.cs在行中Application.Run(new MainForm());:
static void Main()
{
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", AppDomain.CurrentDomain.BaseDirectory + "\\Settings.config");
bool ok;
Mutex m = new Mutex(true, WindowsIdentity.GetCurrent().Name.ToString().Split('\\')[1] + "MyApp", out ok);
if (ok)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// The Error will cause HERE
Application.Run(new MainForm());
GC.KeepAlive(m);
}
else
Application.Exit();
}
Run Code Online (Sandbox Code Playgroud)
MainForm是包含功能区栏的mdi父级.这是完整的堆栈跟踪:
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
at System.Drawing.Image.FromHbitmap(IntPtr hbitmap)
at System.Drawing.Icon.ToBitmap()
at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception …Run Code Online (Sandbox Code Playgroud) 背景:
我正在处理一个非常旧的应用程序,它很少并且非常间歇性地生成异常。
目前的做法:
通常我们程序员使用全局异常处理程序处理罕见的未知数,连接如下:
[STAThread]
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
private static void Main()
{
Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new OldAppWithLotsOfWierdExceptionsThatUsersAlwaysIgnore());
}
private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
{
//-------------------------------
ReportToDevelopers("All the steps & variables you need to repro the problem are: " +
ShowMeStepsToReproduceAndDiagnoseProblem(t));
//-------------------------------
MessageToUser.Show("It’s not you, it’s us. This is our fault.\r\n Detailed information about this error has automatically been recorded and we have been notified.Yes, we do look at …Run Code Online (Sandbox Code Playgroud)