C# Winform中慎用Application.DoEvents

时间:2021-04-02 05:06:00
private void Add()
{
for (int i = ; i < ; i++)
{
Button button = new Button();
button.Width = ;
button.Height = ;
panel1.Controls.Add(button);
Application.DoEvents();//此处用的话会导致问题
}
}
private void button1_Click(object sender, EventArgs e)
{
Add();
}
int errorCount = ;
private void timer1_Tick(object sender, EventArgs e)
{
panel1.Controls.Clear();
Add(); }

如上代码,100ms的定时器,执行几分多钟后就会崩溃,提示*,推测可能是添加Button时队列里有新事件,DoEvents引爆导致。