When is invoke required on GUI objects?

巧了我就是萌 提交于 2019-12-20 02:48:11

问题


Using C# Windows.Forms, do the methods Invalidate(), Refresh(), etc. have to be run on the main/GUI thread (require Invoke/BeginInvoke)? How about changes to members of a GUI object such as adding/deleting Points or changing the Color of a Series in a Charting.Chart object?

I have some of these changes occuring in a worker thread without any issues (so I guess they are ok?), but I'm trying to distinguish which changes are explicity required on the GUI thread and which changes can occur on the object in a worker thread. Does anyone have a link or book reference to guidance on this subject?


回答1:


In general, you should typically assume that ALL changes to GUI elements should be made on the UI thread.

Often, changes on a background thread will work during testing, but break post-deployment. In general, unless otherwise documented, it's much safer to assume that everything should be on the UI thread.




回答2:


You can not make synchronous method calls that manipulate WinForms controls on a worker thread.

I'm not sure what "adding/deleting Points" is referring to, or the Charting.Chart class. This class may encapsulate GUI Thread invocation on their own.

Generally speaking, if you are modifying the size, text, background color, or other properties on a Control, this must use Invoke / BeginInvoke.




回答3:


This should tell you Control.InvokeRequired



来源:https://stackoverflow.com/questions/5554981/when-is-invoke-required-on-gui-objects

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