我試圖使用MSDN的Converting between RTF and HTML庫將一些RTF文本轉換爲HTML。我的設置的jist是從JavaScript到C#處理程序的AJAX調用,它調用此MarkupConverter
庫進行轉換,然後寫回HTML。RichTextBox - InvalidOperationException:調用線程必須是STA
這裏是我的JavaScript:
$.ajax({
type: "POST",
url: "MyHandler.ashx",
data: richTextData,
success: function (html) {
alert('success, html: ' + html);
},
error: function (msg) {
alert("error: " + msg);
}
});
而且從我的處理程序,這也是很簡單的代碼:
public void ProcessRequest(HttpContext context)
{
if (context.Request.Form.Count > 0)
{
string rtf = context.Request.Form[0];
string html = "";
if (rtf != "")
{
markupConverter = new MarkupConverter.MarkupConverter();
html = markupConverter.ConvertRtfToHtml(rtf);
}
if (html != "")
{
context.Response.ContentType = "text/html";
context.Response.Write(html);
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("Error from RTF2HTML");
}
}
}
的問題是,每到這個運行時,一個異常被拋出,因爲RichTextBox
控制在後臺線程上創建:
[InvalidOperationException:調用threa d必須STA,因爲許多 UI組件需要此。]
System.Windows.Input.InputManager..ctor()11032206
System.Windows.Input.InputManager.GetCurrentInputManagerImpl()125個
System.Windows .Input.KeyboardNavigation..ctor()185個
System.Windows.FrameworkElement.EnsureFrameworkServices()109
System.Windows.FrameworkElement..ctor()504
System.Windows.Controls.Control..ctor ()+87
System.Windows.Controls.RichTextBox..ctor(FlowDocument document)+56
MarkupConverter.RtfToHtmlConv erter.ConvertRtfToXaml(字符串rtfText) +67 MarkupConverter.RtfToHtmlConverter.ConvertRtfToHtml(字符串rtfText)+23 MyHandler.ProcessRequest(HttpContext的背景下)416
我想也許是因爲AJAX調用臺異步,呼叫被越來越放置在後臺線程上。所以我改成了這樣:
var postText = $.ajax({
type: "POST",
url: "RTF2HTML.ashx",
data: textData,
async: false
}).responseText;
alert(postText);
但是即便這樣,當我檢查我的處理程序當前線程:
context.Response.Write("thread: " + System.Threading.Thread.CurrentThread.GetApartmentState().ToString());
它仍然返回MTA。
有沒有辦法掛鉤到主STA線程,或者我將不得不創建一個新線程並指定STA?如果是這樣的話,我如何設置回調函數來返回我目前使用的HTML Response.Write
?
執行'thread.Join()'後會執行等待,以便在'ConvertMarkup()'完成後'if(html!='')'行不執行? – lhan 2013-04-24 15:13:10
顯然它!工作!謝謝! – lhan 2013-04-24 15:15:53
沒問題,樂於幫忙! thread.Join自己發信號給調用線程等待,直到線程執行完成...簡單但是完成工作。 – Henners 2013-04-24 15:24:28