1

I'm having a similar problem as this issue. I'm trying to display a WPF window from an Installer class from System.Configuration.Install. My window, corresponding to my software license manager window, should ideally pop up during or after the installation process to install the license as well. However, the installation finishes without the window ever showing and I'm not sure why.

Here is my code:

[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
     public Installer1()
     {
         InitializeComponent();
     }


     // INSTALL EVENT //////////
     public override void Install(System.Collections.IDictionary stateSaver)
     {
         StaThreadWrapper(() =>
         {
             LicenseActivationWindow activationWindow = new LicenseActivationWindow();
             activationWindow.ShowDialog();
         });
     }

     // Method to call the xaml in a thread safe way
     private static void StaThreadWrapper(Action action)
     {
         var t = new Thread(o =>
         {
              action();
              System.Windows.Threading.Dispatcher.Run();
         });
         t.SetApartmentState(ApartmentState.STA);
         t.Start();
     }


     // UNINSTALL EVENT //////////
     public override void Uninstall(System.Collections.IDictionary stateSaver)
     {
     }
}

I had to add the StaThreadWrapper method to fix the "The calling thread must be STA, because many UI components require this." error from calling the wpf window directly from the installer thread and I'm no longer getting the error message but I'm also not getting the window to show up. I thought this would solve the issue like [1] but it didn't.

What am I doing wrong?

SofiaFeist
  • 53
  • 4
  • `ShowDialog()` blocks and prevents `Dispatcher.Run` from being run. Try with `.Show()`. – mm8 Sep 21 '22 at 12:16
  • @mm8 Thank you for your reply. However, I tried that as well and it did not work. Window still refuses to show :( – SofiaFeist Sep 21 '22 at 12:31
  • If you call `var app = Application.Current` inside `Install` method, Will the value be null? – Muhammad Sulaiman Sep 21 '22 at 12:43
  • @MuhammadSulaiman The value is indeed null. – SofiaFeist Sep 21 '22 at 14:29
  • 1
    I suggest you put your wpf window in your wpf app. Show it on first run. The old way of showing a custom view was https://www.codeproject.com/Articles/1028052/Create-custom-dialogs-for-an-Setup-project-in-Visu – Andy Sep 21 '22 at 17:39
  • I would like to avoid that option since my app is not a wpf app; it's a Revit plug-in. But I will keep it mind if I don't find any alternative solution! – SofiaFeist Sep 23 '22 at 11:55

1 Answers1

1

Displaying a window in a System.Configuration.Install.Installer is probably not a very good idea but if you still want to try it out, then create an Application class on the STA thread. Something like this:

var t = new Thread(o =>
{
    var app = new System.Windows.Application();
    app.Run(new LicenseActivationWindow());
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I tried as you suggested but window is still a no show. Would a Windows Forms window work better? How would you suggest I approach this? – SofiaFeist Sep 21 '22 at 14:41