Multi-Threading - problem

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
7 posts Page 1 of 1
Contributors
User avatar
Rhez
Member
Member
Posts: 30
Joined: Sat Jan 04, 2014 7:24 am

Multi-Threading - problem
Rhez
[center]Image[/center]

My code is like this
Code: Select all
Sub Note()
        Try
            Dim TXT1 As String = "http://www.myurl.pro"
            Dim Read1 As StreamReader = New StreamReader(WebClient.OpenRead(TXT1))
            Dim STR1 As String = Read1.ReadToEnd
            Note.Text = STR1
Catch ex As Exception
            Note.Text = "ERROR"
        End Try
End Sub
 Dim Note = New Thread(Sub() Note())
Then on Form Load Event :
Note.Start()

The url there are just a replace of my original url.

I hope someone will help me out of this. Thanks in advance cooll;
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Multi-Threading - problem
comathi
As explained in This StackOverflow thread, controls created from the UI thread can only be accessed through the UI thread (hence the error).

The link also offers a possible solution to your problem, ie. checking if Note() is being called from another thread, and if so calling the appropriate methods from the UI thread itself cooll;
User avatar
Rhez
Member
Member
Posts: 30
Joined: Sat Jan 04, 2014 7:24 am

Re: Multi-Threading - problem
Rhez
I'm sorry for being a newbie.
But, can u give me the exact way/code. I'm new with multi-threading nor multi-threading with invoking.
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Multi-Threading - problem
comathi
I'm also a noob with multi-threading in VB.NET, but from this link, this is what I understand should be used:
Code: Select all
Dim Note = New Thread(AddressOf Note)

Private Delegate Sub NoteDelegate()
Private Sub Note()
    If Me.InvokeRequired Then
        Me.Invoke(New NoteDelegate(AddressOf Note))
    Else
        Try
            Dim TXT1 As String = "http://www.myurl.pro"
            Dim Read1 As StreamReader = New StreamReader(WebClient.OpenRead(TXT1))
            Dim STR1 As String = Read1.ReadToEnd
            Note.Text = STR1
        Catch ex As Exception
            Note.Text = "ERROR"
        End Try
    End If
End Sub
And then of course, as before, you'd have Note.Start() in Form.Load
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

Re: Multi-Threading - problem
SumCode
comathi wrote:
I'm also a noob with multi-threading in VB.NET, but from this link, this is what I understand should be used:
Code: Select all
Dim Note = New Thread(AddressOf Note)

Private Delegate Sub NoteDelegate()
Private Sub Note()
    If Me.InvokeRequired Then
        Me.Invoke(New NoteDelegate(AddressOf Note))
    Else
        Try
            Dim TXT1 As String = "http://www.myurl.pro"
            Dim Read1 As StreamReader = New StreamReader(WebClient.OpenRead(TXT1))
            Dim STR1 As String = Read1.ReadToEnd
            Note.Text = STR1
        Catch ex As Exception
            Note.Text = "ERROR"
        End Try
    End If
End Sub
And then of course, as before, you'd have Note.Start() in Form.Load
You don't have to create a new delegate to invoke your method. You could use a pre-existing delegate called the MethodInvoker
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Multi-Threading - problem
comathi
#SumCode

Care to post a snippet as to how that would work for what #Rhez is trying to do? I haven't touched VB in a while and I haven't done much multithreading, so this is not something I'm familiar with :)
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

Re: Multi-Threading - problem
SumCode
So what I am saying is that instead of creating a new delegate (in your case 'NoteDelegate') you can use a pre made delegate called MethodInvoker. So where you wrote
Code: Select all
Me.Invoke(New NoteDelegate(AddressOf Note))
one could write
Code: Select all
Invoke(New MethodInvoker(AddressOf Note))
Not that big of a deal though. One could use either create a new delegate if one pleases or just use the MethodInvoker (or Action) delegate.
7 posts Page 1 of 1
Return to “Coding Help & Support”