![](/img/trans.png)
[英]Problem accessing a TextBox control from another class/thread in VB.NET
[英]VB.net Print to textbox from another thread
我有以下代碼,並且我想基本上復制一個不斷變化的文本文件的內容並將其轉儲到文本框顯示中,我有以下代碼,但目前無法正常工作。
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
創建一個根據需要使用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())
進行調用。
基於快速參考的答案從VB.net的另一個線程修改控件的屬性 ; 自從我在.NET中編寫多線程UI代碼以來已經有一段時間了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.