开发者

When is invoke required on GUI objects?

开发者 https://www.devze.com 2023-02-22 19:23 出处:网络
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/dele

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开发者_C百科 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?


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.


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.


This should tell you Control.InvokeRequired

0

精彩评论

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