Adblock breaks this site

Issues with multi-threading

Discussion in 'Programming General' started by Blupig, Nov 11, 2009.

  1. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    Issues with multi-threading

    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?
     
  2. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Issues with multi-threading

  3. TheDevelpoer

    TheDevelpoer Newcomer

    Joined:
    Nov 11, 2009
    Posts:
    3
    Referrals:
    0
    Sythe Gold:
    0
    Issues with multi-threading

    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
     
  4. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Issues with multi-threading

    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.
     
  5. Flaming Idiots

    Flaming Idiots Active Member
    Visual Basic Programmers

    Joined:
    Dec 22, 2005
    Posts:
    235
    Referrals:
    1
    Sythe Gold:
    0
    Two Factor Authentication User
    Issues with multi-threading

    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.
     
  6. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    Issues with multi-threading

    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
    
     
  7. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Issues with multi-threading

    Wo. Programming section seems slightly active on this thread.
     
  8. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    Issues with multi-threading

    I've been fucking around a little bit...It's quite difficult >_<
     
  9. erix920

    erix920 Newcomer

    Joined:
    Feb 5, 2010
    Posts:
    14
    Referrals:
    0
    Sythe Gold:
    0
    Issues with multi-threading

    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 ;).
     
  10. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    Issues with multi-threading

    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.
     
  11. erix920

    erix920 Newcomer

    Joined:
    Feb 5, 2010
    Posts:
    14
    Referrals:
    0
    Sythe Gold:
    0
    Issues with multi-threading

    I've used checkforillegalcrossthreadcalls = false on numerous occasions and the program never crashed from using it.
     
  12. crackmyknuckles

    crackmyknuckles Newcomer

    Joined:
    Jan 9, 2009
    Posts:
    15
    Referrals:
    0
    Sythe Gold:
    0
    Issues with multi-threading

    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.
     
< Habbo.ca Browser - By Serpen64 | Creating an exe from a program? >


 
 
Adblock breaks this site