将 Dispatcher 与线程一起使用
Using Dispatcher with thread
我有一个 rtf 字符串列表,需要转换为 html。我正在使用 richtextbox 控件将 rtf 转换为 html。我的问题是 this
该解决方案也应该有效,但我如何在我的代码中实施该解决方案?
public string ConvertRtfToHtml(string rtfText)
{
try
{
var thread = new Thread(ConvertRtfInSTAThread);
var threadData = new ConvertRtfThreadData { RtfText = rtfText };
thread.SetApartmentState(ApartmentState.STA);
thread.Start(threadData);
try
{
thread.Join();
}
catch(ThreadStateException e){
logger.Error("ThreadStateException " + e.Message);
}
catch (ThreadInterruptedException e) {
logger.Error("ThreadInterruptedException " + e.Message);
}
return threadData.HtmlText;
}
catch (Exception e){
logger.Error("ConvertRtfToHtml: " + e.InnerException.Message);
return "Error";
}
}
private void ConvertRtfInSTAThread(object rtf)
{
MarkupConverter.MarkupConverter markupConverter = new MarkupConverter.MarkupConverter();
var threadData = rtf as ConvertRtfThreadData;
try
{
threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
}
catch(Exception e){
logger.Error("ConvertRtfInSTAThread: " + e.Message);
}
}
此 markupconverter.convertrtftohtml 使用 richtextbox 控件。
在上面的代码中,我应该把 Dispatcher 放在哪里?
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();
我是这样使用的
private void ConvertRtfInSTAThread(object rtf)
{
MarkupConverter.MarkupConverter markupConverter = new MarkupConverter.MarkupConverter();
var threadData = rtf as ConvertRtfThreadData;
try
{
threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();
}
catch(Exception e){
logger.Error("ConvertRtfInSTAThread: " + e.Message);
}
}
我有一个 rtf 字符串列表,需要转换为 html。我正在使用 richtextbox 控件将 rtf 转换为 html。我的问题是 this
该解决方案也应该有效,但我如何在我的代码中实施该解决方案?
public string ConvertRtfToHtml(string rtfText)
{
try
{
var thread = new Thread(ConvertRtfInSTAThread);
var threadData = new ConvertRtfThreadData { RtfText = rtfText };
thread.SetApartmentState(ApartmentState.STA);
thread.Start(threadData);
try
{
thread.Join();
}
catch(ThreadStateException e){
logger.Error("ThreadStateException " + e.Message);
}
catch (ThreadInterruptedException e) {
logger.Error("ThreadInterruptedException " + e.Message);
}
return threadData.HtmlText;
}
catch (Exception e){
logger.Error("ConvertRtfToHtml: " + e.InnerException.Message);
return "Error";
}
}
private void ConvertRtfInSTAThread(object rtf)
{
MarkupConverter.MarkupConverter markupConverter = new MarkupConverter.MarkupConverter();
var threadData = rtf as ConvertRtfThreadData;
try
{
threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
}
catch(Exception e){
logger.Error("ConvertRtfInSTAThread: " + e.Message);
}
}
此 markupconverter.convertrtftohtml 使用 richtextbox 控件。
在上面的代码中,我应该把 Dispatcher 放在哪里?
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();
我是这样使用的
private void ConvertRtfInSTAThread(object rtf)
{
MarkupConverter.MarkupConverter markupConverter = new MarkupConverter.MarkupConverter();
var threadData = rtf as ConvertRtfThreadData;
try
{
threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();
}
catch(Exception e){
logger.Error("ConvertRtfInSTAThread: " + e.Message);
}
}