Why not use: System.Diagnostics.Process
?
Process myProcess = new Process();
myProcess.StartInfo.FileName = file;
myProcess.Start();
题
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
解决方案
Why not use: System.Diagnostics.Process
?
Process myProcess = new Process();
myProcess.StartInfo.FileName = file;
myProcess.Start();
其他提示
You need to add the following to your main
method of the application:
[STAThread]
static void Main(string[] args)
{
// ...
This is probably down to you new thread trying to access UI elements and in general only one thread per application can do this.
There isn't a new application, you're loading an assembly into your own app. You can change the apartment model of your thread using Thread.SetApartmentState
: http://msdn.microsoft.com/en-GB/library/system.threading.thread.setapartmentstate.aspx