在后台线程上使用控件

时间:2013-10-31 20:59:32

标签: wpf multithreading

我想使用RichTextBox控件仅用于操作RTF,但是在后台线程上。

控件未以任何方式添加到用户界面,但仍会返回STA错误,因为许多UI组件都需要这个' - 有什么方法可以克服这个问题吗?

由于

 Dim vEditor As New RichTextBox
        'Return the existing log (if it exists)
        strSQL = "SELECT Log FROM Work_Orders WHERE Order_ID = " & Order_ID
        Dim vExistingLog As String = vService.ReturnScalarString(strSQL, Current_HOA_ID)
        If Not vExistingLog = "Empty" Then
            Using MS As New IO.MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(vExistingLog))
                vEditor.Selection.Load(MS, DataFormats.Rtf)
            End Using
        End If
        'Return the caret to the top of the document
        vEditor.CaretPosition = vEditor.Document.ContentStart
        'Add the email text
        Dim LogEntry As String = "Email entry made by " & User_Name & " on " & FullDate(Today) & " at " & Format(Now, "HH:mm") & " UTC" & "~~~"
        LogEntry += "Email sent to " & SendEmail_SupplierName & " at " & SendEmail_EmailAddress & " from " & AnnualDT.Rows(0)("Email_Maintenance") & "~~~ ~~~ ~~~"
        LogEntry += "End of email entry...." & "~~~ ~~~ ~~~"
        vEditor.CaretPosition.InsertTextInRun("Start of email entry ~~~ ~~~ ^^^" & SendEmail_Subject & "### ~~~ ~~~" & TextString & "~~~ ~~~" & LogEntry)

        'Add the divider
        Dim vSeparator As String = "{\pard \brdrb \brdrs \brdrw10 \brsp20 \par}" & Environment.NewLine
        vSeparator += "{\pard\par}"
        Using MS As New IO.MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(vSeparator))
            vEditor.Selection.Load(MS, DataFormats.Rtf)
        End Using

        'change the fontsize to something reasonable
        Dim FullRange As New TextRange(vEditor.Document.ContentStart, vEditor.Document.ContentEnd)
        FullRange.ApplyPropertyValue(TextElement.FontSizeProperty, "10")


        Dim vPath As String = My.Application.Info.DirectoryPath
        If Not IO.Directory.Exists(vPath & "\Temp_Data") Then
            IO.Directory.CreateDirectory(vPath & "\Temp_Data")
        End If
        Dim FullPath As String = vPath & "\Temp_Data\Temp_RTF.rtf"
        Dim vRange As New TextRange(vEditor.Document.ContentStart, vEditor.Document.ContentEnd)
        Using FS As New IO.FileStream(FullPath, IO.FileMode.Create)
            vRange.Save(FS, System.Windows.DataFormats.Rtf)
        End Using
        SendEmail_RTF = System.IO.File.ReadAllText(FullPath)
        'Convert paragraphs
        SendEmail_RTF = SendEmail_RTF.Replace("~~~", "{\pard\par}").Replace("^^^", "\b ").Replace("###", "\b0")


        vEditor = Nothing

0 个答案:

没有答案