A
A
Alexander Kosheverov2015-07-30 20:57:09
.NET
Alexander Kosheverov, 2015-07-30 20:57:09

How to show dialog in BackgroundWorker thread in WPF?

Actually, in order to do this in a Thread, you need to set it to SetApartmentState(ApartmentState.STA); but how can such a thing be done in the background thread of the BackgroundWorker?
There was an idea to create a method in the main window that would show my window through Dispatcher.Invoke, however, I need to return not only DialogResult, but also text fields from the dialog, and for this I need to do either my own return data class, or like in c ++ pass a reference to an object to the function via & (not sure if c# can do this).

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Pukhov, 2015-07-31
@Iworb

I'm not sure if this will help, but you can call it through the dispatcher

Dispatcher.Invoke((Action)delegate() { /* тут можно вызвать диалог
 и вообще делать все что угодно, все это будет в основном потоке */ });

M
MrDywar Pichugin, 2015-07-30
@Dywar

stackoverflow.com/questions/3621150/how-can-i-crea...
"The short answer is, you can't.
Any threads used by BackgroundWorker are MTA threads, because they come from the thread pool. There is no way to change a thread from MTA to STA after it is started."

S
Stanislav Makarov, 2015-07-31
@Nipheris

Can we try to solve a more general problem? Why would you want to show the dialog in a thread? What will it basically give?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question