我正在使用ExcelDna和異步函數。如果在async:d代碼中有一個異常,我想顯示一個奇特的WPF錯誤窗口。我的問題是,我得到錯誤「調用線程必須是STA,因爲許多UI組件需要這個。」我該如何解決這個問題?ExcelDna:Async:調用線程必須是STA
[ExcelFunction(Description = "", Category = "")]
public static async Task<object> /*string*/ Foo(CancellationToken ct)
{
try
{
return await Task.Run(async() =>
{
await Task.Delay(1000, ct);
throw new Exception("BOO");
return "HelloWorld";
}, ct2.Token);
}
catch (Exception e)
{
return ShowWpfErrorWindowThatRequiresSTA(e);
}
}
的可能的複製[「調用線程必須爲STA,因爲許多UI組件都需要這種」創造線程WPF彈出窗口時,錯誤](http://stackoverflow.com/questions/2657212/the-calling -thread-must-sta-because-many-ui-components-require-this-error) – CSharpie
你必須在UI線程上顯示一個窗口。使用Application.Current.Dispatcher.BeginInvoke()。 –
的可能的複製[調用線程必須爲STA,因爲許多UI組件都需要這在WPF(http://stackoverflow.com/questions/4183622/the-calling-thread-must-be-sta-because-many- ui-components-require-this-in-wpf) –