• Log in or sign up for an account here: https://www.sythe.org/login

Issues with multi-threading

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
I'm making a website parser, which parses a website and displays the required information. By doing so on a single thread, the entire application freezes for about 5 seconds, then displays the results. I thought, okay, I should just multi-thread the parsing sub so that the other features of the application can still be used while the parsing is occurring. Since all the application does is grabs information off a website and displays in in labels, lists, etc, I got an error while compiling (with multi-threading) that said that I can't reference an object that wasn't initiated within the current thread. I'm sure that others have made projects that were multi-threaded and required information display in a control, so how would I go about doing that?
 
Member of the Month Winner
Sythe's 10th Anniversary
well if it has to do with delegates i found this code:

Code:
Namespace Delegates

    Namespace RichTextBoxes
        Public Module Container
            Private Delegate Sub DELappendText(ByRef Frm As Form, ByRef Box As RichTextBox, ByRef Text As String)
            Public Sub appendText(ByRef Frm As Form, ByRef Box As RichTextBox, ByRef Text As String)
                If Frm Is Nothing Then Exit Sub
                If Frm.IsDisposed Then Exit Sub
                If Box Is Nothing Then Exit Sub
                If Box.IsDisposed Then Exit Sub

                If Frm.InvokeRequired Then
                    Dim DT As New DELappendText(AddressOf appendText)
                    Frm.Invoke(DT, New Object() {Frm, Box, Text})
                Else
                    Box.Text &= Text
                End If
            End Sub
        End Module
    End Namespace

End Namespace
 
What is the object/data you need in the threads? Shouldn't you just be able to send them copies of the object or if it's just text information they need give them copies of whatever portion of the string.
 
Two Factor Authentication User
What you need to do is use the Control.Invoke method (Dispatcher.Invoke in WPF) to invoke a method that sets your controls properties from their own thread. Here is an example:

Code:
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim thread As New System.Threading.Thread(AddressOf WorkerThread)
    thread.Start(New Action(Of String, String)(AddressOf WorkerThreadCallback))
End Sub

Private Sub WorkerThread(ByVal callback As [Delegate])
    Using client As New System.Net.WebClient
        Dim html = client.DownloadString("http://www.google.com/")
        Dim title = System.Text.RegularExpressions.Regex.Match(html, "<title>([^<]+)</title>").Groups(1).Value
        '' If using WinForms
        Me.BeginInvoke(callback, html, title)
        '' If using WPF
        Me.Dispatcher.BeginInvoke(callback, html, title)
    End Using
End Sub

Private Sub WorkerThreadCallback(ByVal html As String, ByVal title As String)
    Me.HtmlTextBox.Text = html
    Me.TitleLabel.Text = title
End Sub
If you need more information about it, Microsoft has an article about it on MSDN here.
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
Thanks for all the help, I'll take a look later; as for Nullware, I would be doing something along the lines of:

Code:
Thread 1:
Thread.Start()
Thread 2:
Dim s As String = "blah"
Label1.Text = s
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
Wo. Programming section seems slightly active on this thread.
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
I've been fucking around a little bit...It's quite difficult >_<
 
Form1.CheckForIllegalCrossThreadCalls = false?

Of course using delegates is a much more "professional" way of doing things, the above method works just as well.

Also if you are setting a global variable and retrieving the info from another, declare as shared.

If this doesn't work, i'll write some functions to help ya ;).
 
Form1.CheckForIllegalCrossThreadCalls = false?

Of course using delegates is a much more "professional" way of doing things, the above method works just as well.

Also if you are setting a global variable and retrieving the info from another, declare as shared.

If this doesn't work, i'll write some functions to help ya ;).

It's not just a more professional way of doing it. It's the way you should do it. Without delegates, there can be issues with the program which end up causing crashes.
 
I've used checkforillegalcrossthreadcalls = false on numerous occasions and the program never crashed from using it.
 
Control.Invoke is too easy not to use it, not to mention if you really start getting into Multi-Threading you're going to need to learn delegates anyway.
 
Runescape Gamblingrunescape gambling
Back
Top