gpt4 book ai didi

vb.net - 尽管使用了调用,但 UI 线程没有通过线程更新刷新

转载 作者:行者123 更新时间:2023-12-03 13:10:38 25 4
gpt4 key购买 nike

在我想更新 TextBox 的表单上.更新是从一个线程完成的。
线程是这样启动的:

ComThread = New Thread(AddressOf ComPlcVar.Scan_PLC)
ComThread.IsBackground = True
ComThread.SetApartmentState(ApartmentState.STA)
ComThread.Start()
Console.WriteLine("Do start thread")

更新功能是这样的:
If frmAcc.L_Cas_G.InvokeRequired Then
Dim d As New L_Cas_Trait_CallBack(AddressOf L_Cas_Trait)
frmAcc.L_Cas_G.Invoke(d, New Object() {Str})
Else
frmAcc.L_Cas_G.Text = Str & ControlChars.CrLf & " " & DateTime.Now.ToString("dd/MM/yyy") & " " & DateTime.Now.ToString("HH:mm:ss")
End if

声明了函数的委托(delegate)。
 Public Delegate Sub L_Cas_Trait_CallBack(ByVal str As String)
Public Sub L_Cas_Trait(Str As String)

但是当我从线程调用更新函数时,我的 UI 没有更新。

似乎这只是一个 UI 问题,因为当我将 writeline 放在表单的 texchanged 方法上时,我在控制台上得到了一个带有正确字符串显示的事件......
Private Sub L_Cas_G_TextChanged(sender As Object, e As EventArgs) Handles L_Cas_G.TextChanged
Console.WriteLine(L_Cas_G.Text)
End Sub

有人知道我缺少什么吗?

谢谢你。

最佳答案

这可能是因为您在页面的某些地方使用了交叉线程。尝试使用 AppendText 代替。

可能是这样的:

Private Delegate Sub AppendTextBoxDelegate(ByVal TB As TextBox, ByVal txt As String)

Private Sub AppendTextBox(ByVal TB As TextBox, ByVal txt As String)
If TB.InvokeRequired Then
TB.Invoke(New AppendTextBoxDelegate(AddressOf AppendTextBox), New Object() {TB, txt})
Else
TB.AppendText(txt)
End If
End Sub

并按如下方式使用它:
AppendTextBox(YOURTEXTBOX, ADD YOUR CONTENT HERE)

答案来自这里: Update Text Box Properly when Cross-threading in Visual Basic (VS 2012 V11)

请检查页面上的完整示例。祝你好运。

关于vb.net - 尽管使用了调用,但 UI 线程没有通过线程更新刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34339767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com