gpt4 book ai didi

c# - RichTextBox - InvalidOperationException : The calling thread must be STA

转载 作者:行者123 更新时间:2023-12-03 13:20:38 25 4
gpt4 key购买 nike

我正在尝试使用这个 Converting between RTF and HTML来自 MSDN 的库,用于将一些 RTF 文本转换为 HTML。我的设置的要点是从 JavaScript 到 C# 处理程序的 AJAX 调用,该处理程序调用此 MarkupConverter库进行转换,然后写回 HTML。

这是我的 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: The calling thread must be STA, because many UI components require this.]
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.RtfToHtmlConverter.ConvertRtfToXaml(String rtfText) +67 MarkupConverter.RtfToHtmlConverter.ConvertRtfToHtml(String rtfText) +23 MyHandler.ProcessRequest(HttpContext context) +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目前呢?

最佳答案

这可能有用:

How to run something in the STA thread?

也许你可以调用...

html = markupConverter.ConvertRtfToHtml(rtf);

...以相同的方式在不同的线程上?
string rtf;

public void ProcessRequest(HttpContext context)
{
if (context.Request.Form.Count > 0)
{
rtf = context.Request.Form[0];
string html = "";
if (rtf != "")
{
Thread thread = new Thread(ConvertMarkup);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
if (html != "")
{
context.Response.ContentType = "text/html";
context.Response.Write(html);
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("Error from RTF2HTML");
}
}
}

void ConvertMarkup()
{
markupConverter = new MarkupConverter.MarkupConverter();
html = markupConverter.ConvertRtfToHtml(rtf);
}

关于c# - RichTextBox - InvalidOperationException : The calling thread must be STA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16194605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com