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

前端 未结 6 437
粉色の甜心
粉色の甜心 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:47

    Actually, as far as I know, that WAS the plan from the beginning! Every control could be accessed from any thread! And just because thread locking was needed when another thread required access to the control --and because locking is expensive-- a new threading model was crafted called "thread rental". In that model, related controls would be aggregated into "contexts" using only one thread, thus reducing the amount of locking needed. Pretty cool, huh?

    Unfortunately, that attempt was too bold to succeed (and a bit more complex because locking was still required), so the good old Windows Forms threading model --with the single UI thread and with the creating thread to claim ownership of the control-- is used once again in wPF to make our lives ...easier?

提交回复
热议问题