2017-11-01 6 views
0

htmlに変換するために必要なrtfストリングのリストがあります。私はrtfをhtmlに変換するためにrichtextboxコントロールを使用しています。私の問題は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はリッチテキストボックスコントロールを使用します。

上記のコードでDispatcherにはどこが適していますか?

Dispatcher dispatcher = Dispatcher.CurrentDispatcher; 
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal); 
Dispatcher.Run(); 

答えて

0

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); 
     } 

    } 
を次のように私はそれを使用
関連する問題