Why is only the UI thread allowed to modify the UI?

前端 未结 6 447
粉色の甜心
粉色の甜心 2020-11-28 11:59

I know that if I am modifying a control from a different thread, I should take care because WinForms and WPF don\'t allow modifying control\'s state from other threads.

6条回答
  •  没有蜡笔的小新
    2020-11-28 12:24

    Several GUI frameworks have this limitation. According to the book Java Concurrency in Practice the reason for this is to avoid complex locking. The problem is that GUI controls may have to react to both events from the UI, data binding and so forth, which leads to locking from several different sources and thus a risk of deadlocks. To avoid this .NET WinForms (and other UIs) restricts access to components to a single thread and thus avoids locking.

提交回复
热议问题