C# System.Activator.CreateInstance和线程

C# System.Activator.CreateInstance和线程,c#,multithreading,activator,C#,Multithreading,Activator,我正在调用一个外部应用程序,它将XML转换为PDF dynamic generator = null; Assembly a = Assembly.LoadFrom(file); Type type = a.GetType("Application.ConsoleStartup.Program"); generator = Activator.CreateInstance(type); 然后 generator.Run("testXML.xml"); 总的来说,这是可行的。唯一的问题是,

我正在调用一个外部应用程序,它将XML转换为PDF

dynamic generator = null;
Assembly a = Assembly.LoadFrom(file);
Type type = a.GetType("Application.ConsoleStartup.Program");
generator = Activator.CreateInstance(type);
然后

generator.Run("testXML.xml");  
总的来说,这是可行的。唯一的问题是,在某一点上,新打开的应用程序需要STA线程。问题是我无法访问(或非常有限)这个新打开的应用程序。有没有办法绕过这个?请注意,我并不是真正的线程专家

错误如下所示:

error DCP999: [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.Controls.ToolTip..ctor()
   at Application.Parser.Html.Model.Anchor.AfterInsert(IParseContext pc) in C:\work\Common\Main\Source\Parsers\HtmlParser\Model\Anchor.cs:line 31

为什么不使用:
System.Diagnostics.Process

Process myProcess = new Process();
myProcess.StartInfo.FileName = file; 
myProcess.Start();

没有新应用程序,您正在将程序集加载到自己的应用程序中。您可以使用
thread更改线程的单元模型。SetApartmentState

您需要将以下内容添加到应用程序的
main
方法中:

[STAThread]
static void Main(string[] args)
{
    // ...

这可能取决于您尝试访问UI元素的新线程,通常每个应用程序只有一个线程可以做到这一点。

我尝试过,这也是一种方法,但我也需要应用程序提供一些资源(如输出路径,以便打开此PDF)。好的,最后我仍然使用了此线程,并且工作正常。正在使用Arguments属性。此属性的内容将显示在其他进程的主函数的args数组中。这已由主进程设置。但是我没有直接调用Main方法,而是一个run方法。这是我一直在努力的方向,但是我还没有找到连接Activator.CreateInstance(type)和线程的方法