Winform 创建窗口句柄时出错

livesw 2009-02-27 05:14:45
报错处的代码
frm.MdiParent = this;
frm.WindowState = FormWindowState.Maximized;
frm.Show();//出错地点
只是偶尔出错,有时候。

下面是详细的报错信息
未处理 System.OutOfMemoryException
Message="创建窗口句柄时出错。"
Source="System.Windows.Forms"
StackTrace:
在 System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
在 System.Windows.Forms.Control.CreateHandle()
在 System.Windows.Forms.Form.CreateHandle()
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.Show()
在 Win.frmMain.WinOpen(Form frm) 位置 E:\Visual Studio 2008\Projects\LiveSW\Win\Admin\frmMain.cs:行号 91
在 Win.frmMain.签单周报_Click(Object sender, EventArgs e) 位置 E:\Visual Studio 2008\Projects\LiveSW\Win\Admin\frmMain.cs:行号 292
在 DevComponents.DotNetBar.BaseItem.RaiseClick(eEventSource source)
在 DevComponents.DotNetBar.BaseItem.InternalMouseUp(MouseEventArgs objArg)
在 DevComponents.DotNetBar.PopupItem.InternalMouseUp(MouseEventArgs objArg)
在 DevComponents.DotNetBar.ButtonItem.InternalMouseUp(MouseEventArgs objArg)
在 DevComponents.DotNetBar.MenuPanel.OnMouseUp(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 DevComponents.DotNetBar.MenuPanel.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(ApplicationContext context)
在 Win.Program.Main() 位置 E:\Visual Studio 2008\Projects\LiveSW\Win\Program.cs:行号 31
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException: System.NullReferenceException
Message="未将对象引用设置到对象的实例。"
Source="System.Windows.Forms"
StackTrace:
在 System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
在 System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
在 System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
InnerException:
位置 E:\Visual Studio 2008\Projects\LiveSW\Win\Program.cs:行号 31
这个地方的代码:
private static ApplicationContext context;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmSplash sp = new frmSplash();
sp.Show();
context = new ApplicationContext();
context.Tag = sp;
Application.Idle += new EventHandler(Application_Idle);
Application.Run(context);//这个地方就是详细报错的31行
}
//初始化等待处理函数
private static void Application_Idle(object sender, EventArgs e)
{
Application.Idle -= new EventHandler(Application_Idle);
if (context.MainForm == null)
{
frmLogin mw = new frmLogin();
context.MainForm = mw;
//mw.Init();
frmSplash sp = (frmSplash)context.Tag;
sp.Close();
mw.Show();
}
}
请高手指点,只是偶尔出错,另外我用了DotNetBar控件,不是是否有什么影响。
...全文
3582 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
xonln 2011-11-28
  • 打赏
  • 举报
回复
这是Winform不稳定的原因.还是用成熟的技术较好,如Delphi,VC++的Mfc
eleven_123 2011-11-28
  • 打赏
  • 举报
回复
我也遇到这个问题了
//追加
frm.Dispose();
frm = null;
后解决。
希望对后来者有用

www0501 2011-10-31
  • 打赏
  • 举报
回复
我也遇到这样的错误了!是因为创建为父窗体出现的!
卧_槽 2011-10-13
  • 打赏
  • 举报
回复
load事件里面打断点调
caresong 2011-10-13
  • 打赏
  • 举报
回复
但是之前是没问题的
今天就出现问题了
而且是有数据的时候报错,没数据就没问题
萧炎 2011-10-13
  • 打赏
  • 举报
回复
因为窗体程序是单线程的
但是你的主窗口设置了
ismdicontainer=true
主窗口内没有 对象
caresong 2011-10-13
  • 打赏
  • 举报
回复
在主界面加载前用线程调用加载数据
如果没查到数据就没错,查到数据开始初始化窗体就报‘创建窗口句柄时出错。’
zdzcool 2011-10-13
  • 打赏
  • 举报
回复
如果是偶尔出现错误,那就不会是窗口之间是不是设置子窗口的问题。

如果用了其他的第三发控件,先试着去掉看看有没有影响。

这种偶尔错误的情况,只能慢慢的挨个排查了。一下很难定位。
nkhuangyanping 2011-04-07
  • 打赏
  • 举报
回复
我也遇到这样的问题,也是动态生成控件的。还没具体找出原因
hao05010323 2010-12-01
  • 打赏
  • 举报
回复
如果是用dataGridViewX查询数据出错,dataGridViewX.SuspendLayout();即可
Castiel丶Luo 2010-08-26
  • 打赏
  • 举报
回复
...求助啊 同样问题
Castiel丶Luo 2010-08-23
  • 打赏
  • 举报
回复
........楼主 解决没有啊
Lizzy莲 2010-06-18
  • 打赏
  • 举报
回复
遇到相同的问题,也是偶尔出现
http://topic.csdn.net/t/20020913/11/1020251.html
这个LZ解决了,可是没有回复原因和做法
wxm3630478 2010-06-09
  • 打赏
  • 举报
回复
当大量动态生成控件后 就会出这个问题吧 .........句柄用完啦
shighui 2010-06-09
  • 打赏
  • 举报
回复
裁夺 苦苦苦
Tim_Andy 2010-06-09
  • 打赏
  • 举报
回复
我也是用dotnetbar出现了这个错误!苦恼啊
xie19sc 2010-06-09
  • 打赏
  • 举报
回复
4楼的说法是对的,

我也说说我自己的看法,我以前也出现过。

出现句柄出错的原因有内存溢出可能造成,就是说你运行程序,每次点击一次,程序中没有写释放内存的方法,导致系统内存大量累加,达到一定时候就会出现溢出。结果就会出现句柄错误、。

你看看是不是这个原因造成的, 控件集合有没有clear();
Tim_Andy 2010-06-09
  • 打赏
  • 举报
回复
历时两天发现问题所在,视图中自定义列的别名与真名有相同时,用此视图检索数据后再打开新窗口就会捕获该异常
铛铛 2010-06-09
  • 打赏
  • 举报
回复
没遇到过
acidpassion 2010-06-09
  • 打赏
  • 举报
回复
四楼正解
加载更多回复(19)

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧