110,538
社区成员
发帖
与我相关
我的任务
分享
protected bool isexit = false;
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("ddd");
isexit = true;
}
private void button1_Click(object sender, EventArgs e)
{
while (!isexit)
{
Application.DoEvents();
}
}
bool inButtonClick = false;
private void button1_Click(object sender, System.EventArgs e) {
if (inButtonClick) {
MessageBox.Show("Being called when I'm already in here!!!");
}
inButtonClick = true;
for (int i = 0; i < 10000000; i++) {
listBox1.Items.Add(i.ToString());
Application.DoEvents(); // <-- this call here allows the second click to process
}
inButtonClick = false;
}
Application.Run
Process mouse up on button
Fire ButtonClick event
Add some items to listbox
Call Application.DoEvents to make it look more responsive
Process mouse up on close box
Dispose the form
Add remaining items to listbox
Update the first control on the form -- BOOM! Controls collection has been cleared!
This is just one example of codepath gone bad, there’s plenty of others – say the user clicked the button a second time – the button click event would be called again while you’re still processing the first!
protected bool isexit = false;
private void button2_Click(object sender, EventArgs e)
{
Application.DoEvents();
MessageBox.Show("ddd");
isexit = true;
}
private void button1_Click(object sender, EventArgs e)
{
button2.Focus();
while (!isexit)
{
Application.DoEvents();
}
}