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.
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
[center][/center]
My code is like this
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;
My code is like this
Code: Select all
Then on Form Load Event :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())
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;
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;
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;
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.
But, can u give me the exact way/code. I'm new with multi-threading nor multi-threading with invoking.
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
And then of course, as before, you'd have Note.Start() in Form.LoadDim 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
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:You don't have to create a new delegate to invoke your method. You could use a pre-existing delegate called the MethodInvoker
Code: Select allAnd then of course, as before, you'd have Note.Start() in Form.LoadDim 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
#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
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
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
one could write Me.Invoke(New NoteDelegate(AddressOf Note))
Code: Select all
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.Invoke(New MethodInvoker(AddressOf Note))
7 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023