tags:

views:

29

answers:

2

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.

+1  A: 

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.

Veer
private void Window_Loaded(object sender, RoutedEventArgs e) { Thread t = new Thread(ComplexFunction); t.Start(); }and in ComplexFunction(){// doing somethingUpdateStatus("adasd");// doing somethingUpdateStatus("dsad");} It gives runtime error.The calling thread cannot access this object because a different thread owns itUpdateStatus(string status){StatusText.AppendText(status + "\r");}
Jeevan Bhatt
@Jeevan Bhatt: If you're accessing any element created in UIThread then you may need to ask the dispatcher to do the favour for you. `myDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { // Code that accessess UIElement }));` where myDispatcher is a reference to Dispatcher.CurrentDispatcher Set this before calling the thread.
Veer
A: 

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.

decyclone