简体   繁体   English

Dispatcher.BeginInvoke在c#中调用另一个窗口

[英]Dispatcher.BeginInvoke to call another window in c#

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. 在我的wpf应用程序中,我将按钮单击事件作为单独的线程并作为后台进程运行,以便UI响应用户。 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, 在WorkerMethod中,我有一个选项来更改我为用户提供单独窗口的文件名。对于此操作,我使用Dispatcher方法,如下所示,

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. 其中destinationFileName将在SetOutput窗口中设置。 Now come to my issue, when above code executes SetOutput window shows up and doesn't wait until i set the filename. 现在来看我的问题,当上面的代码执行时,SetOutput窗口显示出来并且不等到我设置文件名。 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. 我怎么能保持,直到我在setoutput窗口中单击确定按钮。非常欢迎任何建议。

I used dispatcher.Invoke instead of BeginInvoke. 我使用了dispatcher.Invoke而不是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, 但是当工作方法中的代码继续某一行退出应用程序本身时,请罚款代码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 关心sangeetha

使用ShowDialog()而不是Show()并将输出存储在DialogResult中

 var result = _setOutput.ShowDialog();

You can use Invoke instead of BeginInvoke : 您可以使用Invoke而不是BeginInvoke:

 //Dispatcher.BeginInvoke(showOutput);
 Dispatcher.Invoke(showOutput);

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. 当你在你的动作中使用window.show()方法时,你不会从show方法得到任何结果,你必须调用窗口的show dialog方法,这将强制GUI保持直到对话窗口关闭,然后你将能够从你的对话框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. 这个approch也适合你。 the dispatcher.Invoke will help you out.or you can try DispatcherOperation here. dispatcher.Invoke会帮助你。你可以在这里尝试DispatcherOperation。

try with below changed code. 尝试使用以下更改的代码。

  DispatcherOperation op = Dispatcher.BeginInvoke(showOutput);

                op.Wait();

                if (String.IsNullOrEmpty(output))
                    return;

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: 如果您使用ShowDialog,您可以将值存储在第二个窗口的公共属性中,并可以通过以下方式访问它:

Form2 form2 = new Form2();
if (form2.ShowDialog() == DialogResult.OK)
{
if (form2.ReturnData == "myResult")

... } ...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM