Update Text Box Properly when Cross-threading in Visual Basic (VS 2012 V11)

后端 未结 2 1808
别跟我提以往
别跟我提以往 2021-01-22 15:58
  • Here is my question in short: How do I use the BackGroundWorker (or InvokeRequired method) to make thread-safe calls to append text to a text box?

    <
2条回答
  •  独厮守ぢ
    2021-01-22 16:43

    You can also use SynchronizationContext. Be careful to get a reference to it from the constructor. There's a great article on this at CodeProject.com: http://www.codeproject.com/Articles/14265/The-NET-Framework-s-New-SynchronizationContext-Cla

    Imports System.IO
    Imports System.Threading
    
    Public Class Form1
        Private m_SyncContext As System.Threading.SynchronizationContext
        Private m_DestinationPath As String
    
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            Me.m_SyncContext = System.Threading.SynchronizationContext.Current
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.m_DestinationPath = Me.TextBox2.Text
            Dim directoryPath As String = Path.GetDirectoryName(TextBox1.Text)
            Dim varFileSystemWatcher As New FileSystemWatcher()
            varFileSystemWatcher.Path = directoryPath
    
            varFileSystemWatcher.NotifyFilter = (NotifyFilters.LastWrite)
            varFileSystemWatcher.Filter = Path.GetFileName(TextBox1.Text)
            AddHandler varFileSystemWatcher.Changed, AddressOf OnChanged
            varFileSystemWatcher.EnableRaisingEvents = True
        End Sub
    
        Private Sub OnChanged(source As Object, ByVal e As FileSystemEventArgs)
            My.Computer.FileSystem.CopyFile(e.FullPath, Me.m_DestinationPath & "\" & e.Name, True)
            Me.m_SyncContext.Post(AddressOf UpdateTextBox, "[New Text]")
        End Sub
    
        Private Sub UpdateTextBox(param As Object)
            If (TypeOf (param) Is String) Then
                Me.TextBox3.AppendText(CStr(param))
            End If
        End Sub
    End Class
    

提交回复
热议问题