【问题标题】:How to fix BackgroundWorker1 not doing his work?如何修复 BackgroundWorker1 不做他的工作?
【发布时间】:2019-07-12 17:24:19
【问题描述】:

所以我遇到了 VB.NET 的问题,特别是 BackgroundWorker1。 我有一个名为SystemLoad() 的函数。

当您单击一个按钮时,它会执行BackgroundWorker1.RunWorkerAsync()。这里没问题,但问题出在DoWork 函数上。 在DoWork函数里面,我写SystemLoad()来调用下面的函数。但是,它不起作用。它绝对什么都不做

有解决办法吗?我已经尝试过Dim T As New Thread(AddressOf SystemLoad): T.Start(),但它做了同样的事情,什么都没有

Private Sub SystemLoad()
    Try
        'Log(Prefix & " Resolving target...")
        Main.TextBox2.Invoke(Sub() Main.TextBox2.AppendText(Prefix & " Resolving target..."))

        Using req As New HttpRequest
            req.IgnoreProtocolErrors = True
            req.Cookies = New CookieStorage(False)
            req.UserAgent = Http.RandomUserAgent

            If Main.ComboBox1.SelectedIndex = 0 Then
                Dim P As String = Main.Proxies(New Random().Next(Main.ProxiesCount)).ToString
                req.Proxy = New HttpProxyClient(P.Split(":")(0), P.Split(":")(1))

                'Log(Prefix & " Using proxy " & P)
                Main.TextBox2.Invoke(Sub() Main.TextBox2.AppendText(Prefix & " Using proxy " & P))
            ElseIf Main.ComboBox1.SelectedIndex = 1 Then
                Dim P As String = Main.Proxies(New Random().Next(Main.ProxiesCount)).ToString
                req.Proxy = New Socks4ProxyClient(P.Split(":")(0), P.Split(":")(1))

                'Log(Prefix & " Using proxy " & P)
                Main.TextBox2.Invoke(Sub() Main.TextBox2.AppendText(Prefix & " Using proxy " & P))
            ElseIf Main.ComboBox1.SelectedIndex = 2 Then
                Dim P As String = Main.Proxies(New Random().Next(Main.ProxiesCount)).ToString
                req.Proxy = New Socks5ProxyClient(P.Split(":")(0), P.Split(":")(1))

                'Log(Prefix & " Using proxy " & P)
                Main.TextBox2.Invoke(Sub() Main.TextBox2.AppendText(Prefix & " Using proxy " & P))
            End If

            req.ConnectTimeout = Convert.ToInt32(Link.Split("|")(2))
            req.KeepAliveTimeout = Convert.ToInt32(Link.Split("|")(2))
            req.ReadWriteTimeout = Convert.ToInt32(Link.Split("|")(2))

            Dim Respo As String = req.Post(Link.Split("|")(1)).ToString
            ResolveTarget(Respo)
        End Using

        RefreshTimer.Start()
    Catch ex As Exception
        Main.TextBox2.Invoke(Sub() Main.TextBox2.AppendText(Prefix & " ERROR " & ex.Message))
    End Try
End Sub

这是我声明 BackgroundWorker1 的方式:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BackgroundWorker1.RunWorkerAsync()
End Sub

'SystemLoad() function

Private Sub BackgroundWorker1_DoWork(sender As Object, e As 
System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    BeginInvoke(New Action(AddressOf SystemLoad), Nothing)
End Sub

编辑:我找到了这段代码,但程序 GUI 崩溃了…… 我将此代码放在 BackgroundWorker1_DoWork 函数中,而不是直接放在 Form_Load 函数中,但它仍然崩溃。

BeginInvoke(New Action(AddressOf SystemLoad), Nothing)

【问题讨论】:

  • 有太多的可能性,没有看到代码很难帮助。您是否尝试过断点以查看发生了什么?
  • 我添加了RunWorkerCompleted函数,好像跳过OnWork函数直接进入RunWorkerCompleted函数。
  • 我们需要查看 SystemLoad 代码。
  • 不,在这里发布代码,而不是它的链接。四个空格缩进使其成为代码块。
  • 我刚刚将代码添加到问题中。

标签: .net vb.net


【解决方案1】:

您的以下代码子集有效。

Imports System.ComponentModel

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub SystemLoad()
        Try
            Debug.WriteLine(" Resolving target...")
            ' Here is how to append to TextBox2 from backgrouond thread.
            TextBox2.Invoke(Sub() TextBox2.AppendText("...message..."))
            '...
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
    End Sub

    Dim WithEvents BackgroundWorker1 As New BackgroundWorker

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        SystemLoad()
    End Sub

End Class

如果您没有得到任何日志记录,那么Log() 函数一定失败了。 Log() 函数写入哪里?它是否写入Form 的控件?无法从后台线程更新 Form 或其任何控件。见How do I update the GUI from another thread?

【讨论】:

  • @StyyGital - 您添加的BeginInvoke() 代码只是在与BackgroundWorker 线程不同的另一个线程中运行SystemLoad() 子程序。除非您显示更多代码,例如 Log 子代码,否则无法为您提供帮助。我们只能猜测。
  • Log() 函数就是这样:Main.TextBox2.AppendText(Message & vbNewLine)
  • @StyyGital - 这是它失败的地方。 TextBox2 不能直接从后台线程更新。
  • @StyyGital - 更新代码以显示如何从后台线程追加到 Textbox2
  • 我在 Textbox2.invoke[...] 行添加了一个断点,它转到断点,但即使我删除断点,再次启动,它也不会写入文本框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多