2014-12-13 24 views
0

我有下面的代碼,我想基本上覆制一個不斷變化的文本文件的內容並將其轉儲到文本框顯示,我有下面的代碼,但它不工作在這一刻。VB.net從另一個線程打印到文本框

If Not File.Exists(masterPath) Then 
      File.Create(masterPath).Dispose() 
     End If 
     Try 
      reader = New StreamReader(chatlog) 
     Catch 
      File.Create(chatlog) 
      bool = False 
     End Try 
     If bool Then 
      'Dim writer As New StreamWriter(masterPath) 
      Dim text As String 
      Do Until reader.EndOfStream 
       text = reader.ReadLine() 
       logMenu.AppendText(text & Environment.NewLine) 
      Loop 
      reader.Close() 
      File.Delete(chatlog) 
      File.Create(chatlog).Dispose() 
     End If 
     Thread.Sleep(1000) 
    Loop 

回答

0

創建使用Invoke根據需要,這樣的新方法:

Private Sub DisplayLogLine(ByVal text As String) 
    If logMenu.InvokeRequired Then 
     logMenu.Invoke(AddressOf DisplayLogLine, {text}) 
     Return 
    End If 
    logMenu.AppendText(text & Environment.NewLine) 
End Sub 

然後在循環DisplayLogLine(reader.ReadLine())調用它。

基於快速參考的答案Modifying the properties of controls from another thread in VB.net;自從我在.NET中編寫多線程的UI代碼以來已經有一段時間了。

相關問題