Best way to open new UI window from workflow

Yanıt Best way to open new UI window from workflow

  • 29 Şubat 2012 Çarşamba 11:55
     
      Kod İçerir

    Hi,

    I have a workflow that is running in its own MTA thread and I need to have some activities that will prompt the user for input at various times.

    Attempt 1 (use new STA thread):

    protected override void Execute(CodeActivityContext context)
      Thread t = new Thread(ShowMyDialog);
      t.SetApartmentState(ApartmentState.STA);
                
      t.Start();
      t.Join();
    }
    
    private void ShowMyDialog()
    {
        UI.MyDialog dialog = new UI.MyDialog();
        dialog.ShowDialog();
    }

    So initially I created a new STA thread in the activity and used that to show the window, this all seemed to work ok. I added some UI styling on the main application window using merged resource dictionaries and then I started getting errors about dependency properties and threading issues. Take out the styling and it was ok again. So assume as the resource dictionaries exist on the main thread when WPF applies these on the new UI components things are on the wrong threads.

    Attempt 2 (use main application UI thread):

    After some digging, decided would open the windows using the parent application UI dispatcher like:

    // Show the dialog              
    bool result = (bool)System.Windows.Application.Current.Dispatcher.Invoke((Func<bool>)delegate
    {
      bool dialogresult=false;
    
      // create window and add to queue
      UI.MyDialog dialog = new UI.MyDialog();
      if (dialog.ShowDialog()==true)
      {
        dialogresult = true;
      }
    
      return dialogresult;
    });

    This blocks the main application UI until the dialog is closed, but for me that is ok anyway for this application. Is this the proper way to do it or have a dug up some other problems?

    Thanks

    Chunda


Tüm Yanıtlar