Reputation: 709
In my wpf application i have made a button click event as seperate thread and run as background process so that the UI is responsive to the user. Code as below,
private void btn_convert_Click(object sender, RoutedEventArgs e)
{
//Makes the conversion process as background task which
//makes the UI responsive to the user.
Thread thread = new Thread(new ThreadStart(WorkerMethod));
thread.SetApartmentState(ApartmentState.MTA);
thread.IsBackground = true;
thread.Start();
}
With in the WorkerMethod I have an option to change the filename which i am providing user a separate window.For this action I am using Dispatcher method as below,
if (MessageBox.Show("Do you want to set filename?",
"Information", MessageBoxButton.YesNo, MessageBoxImage.Asterisk) ==
MessageBoxResult.Yes)
{
Action showOutput = () =>
{
BlueBeamConversion.SetOutput _setOutput =
new BlueBeamConversion.SetOutput();
_setOutput.ShowDialog();
};
Dispatcher.BeginInvoke(showOutput);
if (String.IsNullOrEmpty(MainWindow.destinationFileName))
return;
where destinationFileName will be set in SetOutput window. Now come to my issue, when above code executes SetOutput window shows up and doesn't wait until i set the filename. Before setting the filename it comes to the below code,
if (String.IsNullOrEmpty(MainWindow.destinationFileName))
return;
How can i hold until i click ok button in setoutput window.Any suggessions are most welcome.
I used dispatcher.Invoke instead of BeginInvoke. Now it holds the window and takes new name. But when continues the code in workmethod in a certain line it exits the application itself, please fined the code bekow,
if (MessageBox.Show("Do you want to set filename?", "Information", MessageBoxButton.YesNo, MessageBoxImage.Asterisk) == MessageBoxResult.Yes)
{
Action showOutput = () =>
{ BlueBeamConversion.SetOutput _setOutput = new BlueBeamConversion.SetOutput(); _setOutput.ShowDialog(); };
Dispatcher.Invoke(showOutput);
for (int i = 0; i < _listFiles.Items.Count; i++)--- here it exits
{--------- }
Regards sangeetha
Upvotes: 0
Views: 2267
Reputation: 5691
while you are using window.show() method in you action you will not receive any result from show method insteed you have to call the show dialog method of window which will inforce the GUI to hold untill the dialog window is closed and after it you will be able to recive the data from you dialog windo.
Action showOutput = () =>
{ BlueBeamConversion.SetOutput _setOutput = new BlueBeamConversion.SetOutput(); _setOutput.ShowDialog(); };
Dispatcher.BeginInvoke(showOutput);
and the other hand you can wait for the thread to be complete first and till you can wait. this approch will also work for you. the dispatcher.Invoke will help you out.or you can try DispatcherOperation here.
try with below changed code.
DispatcherOperation op = Dispatcher.BeginInvoke(showOutput);
op.Wait();
if (String.IsNullOrEmpty(output))
return;
Upvotes: 0
Reputation: 1018
if you use ShowDialog, you can store the value in a public property of your second window and can access it in a way like this:
Form2 form2 = new Form2();
if (form2.ShowDialog() == DialogResult.OK)
{
if (form2.ReturnData == "myResult")
... }
Upvotes: 0
Reputation: 273264
You can use Invoke instead of BeginInvoke :
//Dispatcher.BeginInvoke(showOutput);
Dispatcher.Invoke(showOutput);
Upvotes: 1
Reputation: 17556
use ShowDialog() instead of Show() and store the output in the DialogResult
var result = _setOutput.ShowDialog();
Upvotes: 1