vb.net - 运行 backgroundworker VB.net 时写入 textbox.text
问题描述
嗨所以我有小表格申请
问题是当我在主线程上运行任务时,程序窗口没有响应
所以我的按钮将任务重定向到后台工作人员:
Private Sub btn_start_Click(sender As Object, e As EventArgs) Handles btn_start.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
现在我想在一个 Sub 中多次输出来自这个 Backgroudworker 的一些文本到我的文本框(txtlog)。我找到了一个允许这样做的解决方案。但是如果我必须多次使用它,这会使代码非常难看:
例子
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If txtLog.InvokeRequired Then
txtLog.Invoke(Sub() txtLog.AppendText("My text" & vbNewLine))
txtLog.Invoke(Sub() txtLog.ScrollToCaret())
Else
txtLog.AppendText("My text" & vbNewLine)
txtLog.ScrollToCaret()
End If
Something in Backgroudworker . . .
If txtLog.InvokeRequired Then
txtLog.Invoke(Sub() txtLog.AppendText("My text" & vbNewLine))
txtLog.Invoke(Sub() txtLog.ScrollToCaret())
Else
txtLog.AppendText("My text" & vbNewLine)
txtLog.ScrollToCaret()
End If
. . .
End Sub
有没有更短的方法可以让我在文本框中写字?
解决方案
使用 a 的全部意义BackgroundWorker
在于您不必调用Invoke
和使用委托。如果你无论如何都要这样做,那BackgroundWorker
是没有意义的。
关于您拥有的代码,InvokeRequired
当您知道DoWork
事件处理程序将在后台线程上执行时,测试的意义何在?它总是会的True
。Invoke
此外,当您可以调用一次并在一次调用中做尽可能多的事情时,为什么还要调用两次并增加所有开销?
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
txtLog.Invoke(Sub()
txtLog.AppendText("My text" & vbNewLine))
txtLog.ScrollToCaret()
End Sub)
正如我所说,你根本不应该打电话Invoke
。如果你想在 UI 线程上做一些事情,BackgroundWorker
那么你调用ReportProgress
并处理ProgressChanged
事件。您要执行的代码进入事件处理程序,您可以ReportProgress
根据需要调用。如果您需要传递数据,您可以使用第二个参数来传递数据,并从事件处理程序中ReportProgress
取回数据。e.UserState
这是我之前准备的一个:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Raise the DoWork event in a worker thread.
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
'This method is executed in a worker thread.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
For i As Integer = 1 To 100
'Raise the ProgressChanged event in the UI thread.
worker.ReportProgress(i, i & " iterations complete")
'Perform some time-consuming operation here.
Threading.Thread.Sleep(250)
Next i
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.Label1.Text = TryCast(e.UserState, String)
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Label1.Text = "Operation complete"
End Sub
推荐阅读
- javascript - Rendering nested interpolation in vuejs. Render interpolation that is in a string that is being rendered through interpolation
- json - How can I get two interface from
- javascript - 使用jquery从复选框中获取价值
- java - 在我的 Androidstudio 音乐播放器暂停按钮问题
- tensorflow - 带有 RNN 的 Keras 损失函数中的梯度
- python - ElementTree find() 总是返回“无” Cisco iOS xe
- javascript - 如何在 JavaScript 中过滤具有最低值的重复项
- c++ - 为什么临时的“测试”没有移动,而只是构建?
- c - 为什么使用 getchar 输入时 putchar 不返回新行?
- openssl - .cert 中的 OpenSSL 主题与 .csr 不同