private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10000; i++)
{
panel1.Controls.Clear();
Panel p = new Panel();
p.BackColor = Color.Red;
p.Dock = DockStyle.Fill;
panel1.Controls.Add(p);
}
}
如上,执行10000个循环,程序会自动退出,无任何提示。
事件查看器有如下提示:
应用程序: TestMenuPop.exe
Framework 版本: v4.0.30319
说明: 由于未经处理的异常,进程终止。
异常信息: System.ComponentModel.Win32Exception
堆栈:
在 System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams)
在 System.Windows.Forms.Control.CreateHandle()
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Control.CreateGraphicsInternal()
在 System.Windows.Forms.ThreadExceptionDialog..ctor(System.Exception)
在 System.Windows.Forms.Application+ThreadContext.OnThreadException(System.Exception)
在 System.Windows.Forms.Control.WndProcException(System.Exception)
在 System.Windows.Forms.Control+ControlNativeWindow.OnThreadException(System.Exception)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
在 System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
在 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
在 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
在 System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
在 TestMenuPop.Program.Main() 请教原因,谢谢。
{
for (int i = 0; i < 10000; i++)
{
panel1.Controls.Clear();
Panel p = new Panel();
p.BackColor = Color.Red;
p.Dock = DockStyle.Fill;
panel1.Controls.Add(p);
}
}
如上,执行10000个循环,程序会自动退出,无任何提示。
事件查看器有如下提示:
应用程序: TestMenuPop.exe
Framework 版本: v4.0.30319
说明: 由于未经处理的异常,进程终止。
异常信息: System.ComponentModel.Win32Exception
堆栈:
在 System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams)
在 System.Windows.Forms.Control.CreateHandle()
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Control.CreateGraphicsInternal()
在 System.Windows.Forms.ThreadExceptionDialog..ctor(System.Exception)
在 System.Windows.Forms.Application+ThreadContext.OnThreadException(System.Exception)
在 System.Windows.Forms.Control.WndProcException(System.Exception)
在 System.Windows.Forms.Control+ControlNativeWindow.OnThreadException(System.Exception)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
在 System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
在 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
在 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
在 System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
在 TestMenuPop.Program.Main() 请教原因,谢谢。
{
panel1.Controls.Clear();
Panel p = new Panel();
p.BackColor = Color.Red;
p.Dock = DockStyle.Fill;
panel1.Controls.Add(p);
}
这有什么意义?再说了你一直new 10000次啊那可以 new一次占1Kb内存吧 那得占多少啊 new是需要分配内存的,系统不崩溃啊兄弟,出现这样的错估计.net框架就捕获这样的异常信息了`然后给你抛出
用GC清理占的资源再试一下
panel1.Controls.Add(p);
下面加上
GC.Collect();个人认为是这个原因 你可以减少到100试试如果没事的话就是它了` C#又没有限制循环多少次
我能想到的就是这个原因 你试试吧
{
panel1.Controls.Clear();
Panel p = null;
for (int i = 0; i < 10000; i++)
{
p = new Panel();
p.BackColor = Color.Red;
p.Dock = DockStyle.Fill;
panel1.Controls.Add(p);
}
}
Panel p = null;
for (int i = 0; i < 10000; i++)
{
p = new Panel();
p.BackColor = Color.Red;
p.Dock = DockStyle.Fill;
panel1.Controls.Add(p);
}
效率肯定比你的写法高一点点而已,你的内存在for循环的时候还是被new消耗掉了你现在的逻辑没有降低资源消耗的好的办法,你将你的要求贴上来,看看有无更好的办法来替代你的循环。
但确实没意义。
可能的问题是内存不够造成溢出了。我做的项目也遇到过,开了25个窗体,直接暴GC.Collect();可能有帮助吧。
代码改为: private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
Panel p = null;
for (int i = 0; i < 10000; i++)
{
p = new Panel();
p.BackColor = Color.Red;
p.Dock = DockStyle.Fill;
panel1.Controls.Add(p);
GC.Collect();
}
}但不一定好使,毕竟内存开销太大了
别的问题没有`你可以试一下4楼的方法