开发者

Why use BeginInvoke here?

I am looking into someone else's code and do not have much开发者_JAVA百科 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.


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."


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜