2013-04-26 31 views
1

我打電話給外部應用程序,它將XML轉換爲PDF。System.Activator.CreateInstance和線程

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 

回答

1

爲什麼不使用:System.Diagnostics.Process

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

我試過了,它也是這樣做的,但我也需要來自應用程序的一些資源(如輸出路徑以打開此PDF)。 – 2013-04-26 07:59:33

+0

那麼,最後我仍然使用這個,它工作正常。謝謝 – 2013-04-26 08:14:48

+0

嘗試使用參數屬性。此屬性的內容將顯示爲您的其他進程的主要功能的參數。 – 2013-04-26 08:48:37

1

您需要添加以下到您main方法的應用程序:

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

這可能是到你新的線程試圖訪問UI元素,並且通常每個應用程序只能有一個線程可以執

+0

這已由Main設置。但我不直接調用Main方法,而是調用Run方法 – 2013-04-26 08:00:36