Search code examples
c#multithreadingactivator

System.Activator.CreateInstance and threading


I am calling an outside application, which turns an XML into a PDF.

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

and then

generator.Run("testXML.xml");  

And in general the thing works. The only problem is, that at a certain point the newly opened application needs the STA thread. The problem is that I have no access (or very limited) to this newly opened application. Is there a way to bypass this? Note that I am not really an expert in threading.

The error goes like this:

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

Solution

  • Why not use: System.Diagnostics.Process?

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