What's wrong with calling Invoke, regardless of InvokeRequired?

前端 未结 6 1507
春和景丽
春和景丽 2020-12-08 13:38

I\'ve seen the common setup for cross threading access to a GUI control, such as discussed here: Shortest way to write a thread-safe access method to a windows forms control

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 14:01

    From non-UI threads we can't touch the UI - very bad things can happen, since controls have thread affinity. So from a non-UI thread we must (at a minumum) call Invoke or BeginInvoke.

    For UI-threads, however - we don't want to call Invoke lots of time; the issue is that if you are already on the UI thread, it still has the unnecessary overhead of sending a message to the form's pump and processing it.

    In reality, in most threading code you know you expect a specific method to be called on a non-UI thread, so in those cases, there is no additional overhead: just call Invoke.

提交回复
热议问题