Is DoEvents() also evil in FormClosing?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 10:35:20

问题


On a different question I suggested to use

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    while (processingLock)
        Application.DoEvents();
}

in the FormClosing event (with processingLock becoming false immediately after a lengthy calculation was done). It should serve the purpose to have the application close down in an orderly manner and the original poster didn't use threads.

On my suggested answer somebody commented that DoEvents is never good, providing this link: Use of Application.DoEvents()

I read and (I think) understood the issue. In my opinion (which is still noobish) there is no risk for confusing events to happen if I use DoEvents as above in the FormClosing event - if there is no way to cancel the closing for the user (like setting e.Cancel = true).

My question: is this a valid method to make sure an application is terminated well and if not, why?


回答1:


Thanks to comments and more thinking about the relevant posts I've figured out why this "special case" with FormClosing is not special after all.

The problem is that the form is not disabled during the FormClosing event. The user can not only try to close the form several times (which would be okay in this scenario), but she can also click any other UI elements - leading to the problems with uncertain execution order etc. that are mentioned in the linked article.

Hard concept for noobs, thanks for you input.



来源:https://stackoverflow.com/questions/25482094/is-doevents-also-evil-in-formclosing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!