0
Reply

WPF window build dll COM VBA

Ask a question
Peter Toth

Peter Toth

11y
1.6k
1
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();
}

}