0

I have a list of rtf strings that are needed to convert to html. I am using a richtextbox control to convert rtf to html. My problem is this

The solution should also work but how do i implement this solution in my code?

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

    }

this markupconverter.convertrtftohtml uses richtextbox control.

Where do i fit the Dispatcher in above code?

 Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
Dispatcher.Run();
Samra
  • 1,815
  • 4
  • 35
  • 71

1 Answers1

2

I used it as follows

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

    }
Samra
  • 1,815
  • 4
  • 35
  • 71