Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a WPF application in a new AppDomain? ExecuteAssembly fails

I'm trying to launch a WPF application from a Console application, using Application Domains, but when I do, I receive unexpected errors.

Running the WPF application standalone, works.

This code works, too:

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var path = string.Format("{0}AddressbookDesktop.exe", baseDirectory);
var processInfo = new ProcessStartInfo(path, "");
Process.Start(processInfo);    

But this code fails with the error below. The error appears to be in the constructor, which is empty:

var addressbookDomain = AppDomain.CreateDomain("addressbookDomain");
addressbookDomain.ExecuteAssembly("AddressbookDesktop.exe");

Stack trace:

System.Windows.Markup.XamlParseException: Cannot create instance of 
'AddressbookMainWindow' defined in assembly 'AddressbookDesktop, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null'. Exception has been thrown
by the target of an invocation. Error in markup file 'AddressbookMainWindow.xaml' Line     1 Position 9.
---> System.Reflection.TargetInvocationException: Exception has been thrown by the
target of an invocation. ---> System.InvalidOperationException: The calling thread must 
be STA, because many UI components require this.

at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.InputManager.get_Current()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Control..ctor()
at System.Windows.Controls.ContentControl..ctor()
at System.Windows.Window..ctor()
at XX.YY.AddressbookDesktop.AddressbookMainWindow..ctor() in      C:\.....\AddressBookDesktop\AddressbookMainWindow.xaml.cs:line 15
--- End of inner exception stack trace ---

I guess I'm doing something wrong, but can't understand what it is. Thanks for any help.

like image 574
Matteo Caprari Avatar asked Aug 05 '10 10:08

Matteo Caprari


People also ask

Where does the execution start in a WPF application?

For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App. g. cs (generated code). In the default project, this is the public static void App.

What is the use of AppDomain in C#?

The AppDomain class implements a set of events that enable applications to respond when an assembly is loaded, when an application domain will be unloaded, or when an unhandled exception is thrown.

What is AppDomain and CurrentDomain?

The CurrentDomain property is used to obtain an AppDomain object that represents the current application domain. The FriendlyName property provides the name of the current application domain, which is then displayed at the command line.

How does an AppDomain get created?

AppDomains are created by the . Net runtime when a managed application is initialised. When you start ABC. EXE, it gets an application domain.


1 Answers

The problem is that WPF must be run from a STA thread (one of the inner exceptions above states this). I got this to work by adding the STAThreadAttribute to my Main() method:

using System;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Console.WriteLine("Starting WpfApplication1.exe...");

        var domain = AppDomain.CreateDomain("WpfApplication1Domain");
        try
        {
            domain.ExecuteAssembly("WpfApplication1.exe");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            AppDomain.Unload(domain);
        }

        Console.WriteLine("WpfApplication1.exe exited, exiting now.");
    }
}
like image 162
Andy Avatar answered Oct 02 '22 08:10

Andy