evanwon / WPFCustomMessageBox

A WPF clone of the native Windows/.NET MessageBox with extra features like custom button text.
122 stars 63 forks source link

Query in Thread #22

Open ArshadKather opened 1 year ago

ArshadKather commented 1 year ago

Can you explain why we need to invoke Message Box in STA. I'm trying to re-create your code for learning purpose.

kcl93 commented 1 year ago

Hello @ArshadKather, Sorry for the late reply! In general most WPF elements are only accessible from a single thread and must not be accessed from multiple different threads in parallel. Because of this we have to ensure that the thread that creates and shows the WPF message box is in the single threaded apartment. A better explanation can be found here: https://stackoverflow.com/questions/2329978/the-calling-thread-must-be-sta-because-many-ui-components-require-this

Kind regards,

kcl93