Peter Toth

Peter Toth

  • NA
  • 1
  • 2.2k

WPF window build dll COM VBA

Jul 17 2013 1:14 PM
Hello,
I wrote a little COM-visible dll which can show a WPF window. I set my dll in a VBA script reference (excel -> VBA) and when I run first time everything is OK, after that the VBA sub is finished and I try to run this script again and I get an error message:

"Cannot create more than one System.Windows.Application instance in the same AppDomain."

the relevant part of code:

class Program1
{

private void ShowWindow()
        {
            _MainWindow = new MainWindow();
            Application app = new Application();
            app.Run(_MainWindow);
            app.Shutdown();

        }
public void StartWindow (){
            appthread = new Thread(ShowWindow);
            appthread.Name = "wth";
            appthread.SetApartmentState(ApartmentState.STA);
            appthread.IsBackground = true;
            appthread.Start();
}

}