Issues with multi-threading

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

Issues with multi-threading
  1. Unread #1 - Nov 11, 2009 at 3:48 PM
  2. Blupig
    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

    Blupig BEEF TOILET
    $5 USD Donor

    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?
     
  3. Unread #2 - Nov 11, 2009 at 4:37 PM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Issues with multi-threading

  5. Unread #3 - Nov 11, 2009 at 5:18 PM
  6. TheDevelpoer
    Joined:
    Nov 11, 2009
    Posts:
    3
    Referrals:
    0
    Sythe Gold:
    0

    TheDevelpoer Newcomer

    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
     
  7. Unread #4 - Nov 11, 2009 at 11:17 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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.
     
  9. Unread #5 - Nov 12, 2009 at 12:02 AM
  10. Flaming Idiots
    Joined:
    Dec 22, 2005
    Posts:
    235
    Referrals:
    1
    Sythe Gold:
    0
    Two Factor Authentication User

    Flaming Idiots Active Member
    Visual Basic Programmers

    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.
     
  11. Unread #6 - Nov 12, 2009 at 10:04 AM
  12. Blupig
    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

    Blupig BEEF TOILET
    $5 USD Donor

    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
    
     
  13. Unread #7 - Nov 12, 2009 at 10:08 AM
  14. SuF
    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

    SuF Legend
    Pirate Retired Global Moderator

    Issues with multi-threading

    Wo. Programming section seems slightly active on this thread.
     
  15. Unread #8 - Nov 29, 2009 at 5:27 PM
  16. Blupig
    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

    Blupig BEEF TOILET
    $5 USD Donor

    Issues with multi-threading

    I've been fucking around a little bit...It's quite difficult >_<
     
  17. Unread #9 - Feb 7, 2010 at 12:25 AM
  18. erix920
    Joined:
    Feb 5, 2010
    Posts:
    14
    Referrals:
    0
    Sythe Gold:
    0

    erix920 Newcomer

    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 ;).
     
  19. Unread #10 - Feb 8, 2010 at 12:15 PM
  20. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    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.
     
  21. Unread #11 - Feb 8, 2010 at 4:41 PM
  22. erix920
    Joined:
    Feb 5, 2010
    Posts:
    14
    Referrals:
    0
    Sythe Gold:
    0

    erix920 Newcomer

    Issues with multi-threading

    I've used checkforillegalcrossthreadcalls = false on numerous occasions and the program never crashed from using it.
     
  23. Unread #12 - Feb 10, 2010 at 11:24 PM
  24. crackmyknuckles
    Joined:
    Jan 9, 2009
    Posts:
    15
    Referrals:
    0
    Sythe Gold:
    0

    crackmyknuckles Newcomer

    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? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site