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

后端 未结 1 544
醉梦人生
醉梦人生 2021-02-09 11:28

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 st

相关标签:
1条回答
  • 2021-02-09 11:56

    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.");
        }
    }
    
    0 讨论(0)
提交回复
热议问题