使用带有线程的Dispatcher

时间:2017-11-01 02:43:00

标签: asp.net-mvc-4 thread-safety richtextbox dispatcher

我有一个转换为html所需的rtf字符串列表。我使用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();

1 个答案:

答案 0 :(得分:0)

我用它如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:background="#dedede"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:layout_marginTop="30dp"
                    android:text="LinearLayout" />

            </LinearLayout>

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </WebView>
        </LinearLayout>

    </ScrollView>

</LinearLayout>