【发布时间】: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 代码。
-
不,在这里发布代码,而不是它的链接。四个空格缩进使其成为代码块。
-
我刚刚将代码添加到问题中。