Jeevan Bhatt
Jeevan Bhatt

Reputation: 6101

How to open and update WPF window by threading

I have Two WPF window Window1.xaml and window2.xaml. In window2.xaml.cs i have one function which does some complex calculation and display intermediate resuls on a TextBlock.

Now What i want. by clicking on button of Window1.xaml i want to open Window2.xaml as a dialog box and want to execute complex function of windows2.xaml.

If i call complex button on Window2.xaml' load event then dilog box apear after execution of complex function.

How to do this by Threading.

Upvotes: 0

Views: 361

Answers (2)

decyclone
decyclone

Reputation: 30810

Opening a Window in a thread that is not UI thread is not recommended at all. You can open a popup window using Show() (modeless) method or ShowDialog() (modal) method. In Window2's load method, you can start a new thread which does a complex operation and once it is done with the operation, it can show a dialog.

Now, while in a different thread than UI thread, if you want to show any UI (dialogs, windows) or change existing UI (textblock, etc), you must use Dispatcher.Invoke method.

Upvotes: 0

Amsakanna
Amsakanna

Reputation: 12934

You need not open the window in a thread. Once you've opened Window2, start a thread for the complex function in the Window2.Loaded event handler. This will leave your UI undisturbed.

Upvotes: 1

Related Questions