Why use BeginInvoke here?

六眼飞鱼酱① 提交于 2019-12-10 16:46:52

问题


I am looking into someone else's code and do not have much experience with anything to do with multi-threading. I came across this line of code:

BeginInvoke((MethodInvoker)delegate() { btnCalibrate.PerformClick(); });

I was wondering why do this when just this would have worked: btnCalibrate.PerformClick();

Thanks for your answers.


回答1:


Because if this code ran in a different thread from the one that created the button in the GUI, an exception would be thrown. Background threads cannot directly call methods on the GUI because the GUI is not thread safe.

Of course, this is just the rational reason for using BeginInvoke! But it is not unusual to find code that contains incantations or magic spells that have just been put in for no good reason, because the author had seen another example that did it that way and so assumed it must be necessary in all cases. If the code you're working with is single threaded, then it is unnecessary.

In particular, Windows Forms itself is resolutely single-threaded. All operations on all windows and controls happen on the same thread, and all their events fire on that same thread. The use of the one GUI thread is partitioned out through a message loop that continuously runs on the thread and reads messages from a queue. The purpose of BeginInvoke is ultimately to post a message to that queue, effectively saying "When you have a moment, please run this chunk of code."




回答2:


To expand on @Earwicker a bit - Control.BeginInvoke is a way to transfer a call from one thread to the thread which owns the Control instance. In the background, it is using Win32 messages via a Win32 function called PostMessage to marshal the call to the method PerformClick on the owning thread. This is needed due to Win32 Windows, and by extension WinForms Controls, being safe to access only from the thread that created it, which is usually the single thread running the GUI.

The reason BeginInvoke is used vs. Invoke is because the former uses PostMessage and returns right away, while the latter uses SendMessage under the covers, and needs to wait PostMessage and waits for the reply from the GUI thread. This can delay the thread making the call or even lockup the application, so BeginInvoke is the better choice here.



来源:https://stackoverflow.com/questions/2286848/why-use-begininvoke-here

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