views:

130

answers:

3

Hi All,

I need to create a Form in a different thread and keep it ranning until a user performe some action in the main thread (click a button).

It is not very difficult to do using

System.Windows.Forms.Application.Run(new ApplicationContext());

that starts an application message loop in the current thread. But this solution requires the usage of System.Windows.Forms namespace, which in not a wpf namespace.

Do you know a wpf-way of achieving this goal? =)

P.S. without starting an application message loop the thread will be imidiatly terminated after processing of the last expression in it. So, the Form will appear only for a moment and will be closed. =(

+1  A: 

Use System.Windows.Threading.Dispatcher.Run()

Nir
A: 

Does this help ?

Application.DoEvents in WPF Revisited

Gishu
No, it doesn't =(
+2  A: 

And, there's also Dispatcher.PushFrame

This is handy because it allows you to run the messageloop until a stop criterion you define, for instance while a progress dialog is on the screen.

jdv
Very interesting method. Do you have any example of using it?
I don't have anything handy, I used it at my previous company. I think I usually created the DispatcherFrame, then create the dialog window, give it a reference to the DispatcherFrame object, and then call PushFrame. When processing completes, or the user cancels, you set the Continue property of the DispatcherFrame to false. In threaded code, I'd first signal the background thread to cancel, and wait for it to signal the UI thread that it is indeed cancelled, and do Continue = false on that event.
jdv