-4

So I have a problem with VB.NET, and specifically the BackgroundWorker1. I have a function named SystemLoad().

When you click a button, it does BackgroundWorker1.RunWorkerAsync(). No problem here, but the problem is on the DoWork function. Inside the DoWork function, I write SystemLoad() to call the following function. But, it doesn't work. It does absolutely nothing.

Is there a solution to this? I already tried doing Dim T As New Thread(AddressOf SystemLoad): T.Start() but it does the same thing, nothing.

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

Here is how I declare the 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

EDIT : I found this code, but the program GUI is crashing ... I put this code in the BackgroundWorker1_DoWork function, not directly in the Form_Load function but it still crashes.

BeginInvoke(New Action(AddressOf SystemLoad), Nothing)
StyyGital
  • 37
  • 1
  • 6
  • There's too many possibilities, without seeing the code it's very hard to help. Have you tried break point to see what is happening? – the_lotus Jul 12 '19 at 17:31
  • I added the RunWorkerCompleted function, and it seems that it skips the `OnWork` function to go directly to the `RunWorkerCompleted` function. – StyyGital Jul 12 '19 at 17:33
  • We need to see the SystemLoad code. – LarsTech Jul 12 '19 at 17:37
  • https://pastebin.com/fStN7MNi – StyyGital Jul 12 '19 at 17:39
  • 2
    No, post the code here, not a link to it. Four space indent to make it a code block. – LarsTech Jul 12 '19 at 17:43
  • Comments doesn't support this type of markdown, so go to the link. :/ – StyyGital Jul 12 '19 at 17:44
  • There is an edit link underneath your question. – LarsTech Jul 12 '19 at 17:45
  • I tried to do [this](pastebin.com/fStN7MNi) that's why. – StyyGital Jul 12 '19 at 17:51
  • does SystemLoad work synchronously...and does SystemLoad do user interface updates? – Ctznkane525 Jul 12 '19 at 17:51
  • It works synchronously, but the problem is that I don't know how to make it asynchronous, that's why :/ And user interface updates, like `Application.DoEvents()`? – StyyGital Jul 12 '19 at 17:56
  • If SystemLoad works on its own and when in the BackgroundWork doesn't even log, that means it's possible the event isn't properly hooked. We need to see how you create the background worker and hook it up to SystemLoad – the_lotus Jul 12 '19 at 18:01
  • https://pastebin.com/Y3b5GmYY Note that I create the BackgroundWorker through the GUI. – StyyGital Jul 12 '19 at 18:06
  • @StyyGital I can't see those links here, they are blocked. – the_lotus Jul 12 '19 at 18:07
  • Y3b5GmYY - It's on pastebin. – StyyGital Jul 12 '19 at 18:08
  • We are happy to help you, and voluntarily, but it's required to post the code here. If that Pastebin URL goes bad (data is purged/company out of business), then all people have is the question without any code. – Jimmy Smith Jul 12 '19 at 18:28
  • 2
    I just added the code to the question. – StyyGital Jul 12 '19 at 18:30
  • It looks like it should be working. When you start the app, put a breakpoint at `Log(Prefix & " Resolving target...")`. Is it calling that function and reaching the breakpoint? – Jimmy Smith Jul 12 '19 at 18:43
  • `When you click a button, it does BackgroundWorker1.RunWorkerAsync()` That's not really true. You have it running an a load event, not a click event. Easiest way to debug this is try running SystemLoad without the background thread and use the debugger to see what is happening. – LarsTech Jul 12 '19 at 18:44
  • I was wrong, it was on a load event and not a click event. Bruh. And it stops to the breakpoint, but when I remove it, it won't even log... – StyyGital Jul 12 '19 at 18:55
  • Like I said, don't run this code in a separate thread. Even remove the Try-Catch since you want to see what exceptions are happening so you can fix them. – LarsTech Jul 12 '19 at 19:58
  • I already tried to remove the Try-Catch, it does the same thing. – StyyGital Jul 13 '19 at 10:11
  • I edited the code too. – StyyGital Jul 13 '19 at 10:41
  • Pretty harsh way to learn that you need to include your code with questions. This site can be cruel to newbies. – Jeremy Thompson Jun 09 '21 at 04:22

1 Answers1

0

The following subset of your code works.

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

If you do not get any logging, then the Log() function must be failing. Where is the Log() function writing to? Is it writing to a control of the Form? A Form or any of its controls cannot be updated from a background thread. See How do I update the GUI from another thread?.

RobertBaron
  • 2,817
  • 1
  • 12
  • 19
  • @StyyGital - The `BeginInvoke()` code that you added just runs the `SystemLoad()` sub in another thread different from the `BackgroundWorker` thread. Unless you show more of your code, like the `Log` sub, it is not possible to help you. We can only guess. – RobertBaron Jul 13 '19 at 12:47
  • The `Log()` function is just that : `Main.TextBox2.AppendText(Message & vbNewLine)` – StyyGital Jul 13 '19 at 19:22
  • @StyyGital - This is where it fails. The `TextBox2` cannot be updated directly from a background thread. – RobertBaron Jul 13 '19 at 19:34
  • @StyyGital - Updated code to show how to append to `Textbox2` from background thread. – RobertBaron Jul 13 '19 at 19:54
  • I added a breakpoint to the line Textbox2.invoke[...] and it GOES to the breakpoint, but still even if i remove the breakpoint, launch again, it wont write in the textbox. – StyyGital Jul 14 '19 at 13:09
  • @StyyGital - The code that I posted definitely works. Please post your code. – RobertBaron Jul 14 '19 at 15:41
  • @StyyGital - This should be working, the only unknowns for me because not all the code is shown are `Prefix` and `Main`. There must be an error somewhere else. Copy my code, make sure it works, then modify it to get to your code, making sure it still works whenever you add something to it. – RobertBaron Jul 14 '19 at 18:40