C# Winform中慎用Application.DoEvents
private void Add() { for (int i = 0; i < 100; i++) { Button button = new Button(); button.Width = 300; button.Height = 200; panel1.Controls.Add(button); Application.DoEvents();//此处用的话会导致问题 } } private void button1_Click(object sender, EventArgs e) { Add(); } int errorCount = 0; private void timer1_Tick(object sender, EventArgs e) { panel1.Controls.Clear(); Add(); }
如上代码,100ms的定时器,执行几分多钟后就会崩溃,提示StackOverFlow,推测可能是添加Button时队列里有新事件,DoEvents引爆导致。
本博客是个人工作中记录,更深层次的问题可以提供有偿技术支持。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。