Because if this code works in a different thread with the one that created the button in the GUI, an exception will be thrown. Background threads cannot directly invoke methods in the GUI because the GUI is not thread safe.
Of course, this is just a reasonable reason to use BeginInvoke ! But itβs not unusual to find a code that contains spells or magic spells that have just been entered for no good reason, because the author saw another example that did it this way, and therefore suggested that it should be necessary all cases. If the code you are working with is single-threaded, then it is not needed.
In particular, Windows Forms itself is single-threaded. All operations on all windows and controls occur in one thread, and all their events fire in the same thread. The use of a single GUI thread is shared through a message loop that runs continuously in the thread and reads messages from the queue. BeginInvoke's goal is to ultimately send a message to this queue, effectively saying, "When you have a moment, run this piece of code."
source share