Klaus78
Klaus78

Reputation: 11906

Window: graphic element on the same thread (STA)

Why in Windows a graphical element (es a Button in Winform) can be accessed only from the thread that created it?

Upvotes: 0

Views: 48

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564751

This is due to the fact that most native windowing APIs, such as the Windows API, have thread affinity built in to allow the messaging to work properly, as the "messages" (such as input events) are processed in a queue within a single thread (ie: the message pump).

As such, the graphical elements that wrap this functionality, such as Windows Forms, inherit this limitation from their core API.

That being said, some frameworks don't limit you in this fashion. For example, WPF allows data bound primitives to change from background threads, and the 4.5 build will even allow items within collections to be changed from background threads. The thread synchronization requirements get handled internally.

Upvotes: 2

Related Questions