problem with thread safe changing controls-agian
now that ive changed my code around i can no longer change a control on my main form from a class thats being run on a different thread.
my onload event has this:
Code:
WC = New Class2
ThreadingPool.QueueUserWorkItem(AddressOf WC.FindMyPort)
which runs the wc.findmyport sub in the class2 class. i tried using this:
Code:
Private Delegate Function GetControlTextInvoker(ByVal ctl As Control) As String Private Function GetControlText(ByVal ctl As Control) As String
Dim text As String
If ctl.InvokeRequired Then
text = CStr(ctl.Invoke(New GetControlTextInvoker(AddressOf GetControlText), ctl))
Else
text = ctl.Text
End If
Return textEnd Function
but its not longer working. i assume because im working with two different classes. ive tried changing everything from private to public. what i want to do is change a label's text on my main form from my class2 class being run on a seperate thread
th
Re: problem with thread safe changing controls-agian
Um, you say that you want to change the Text on a Label, which would mean setting it, yet you're using a method named GetControlText. Do you see an issue there? At no point does that GetControlText method change any property of the control you pass to it. I think you've just ripped that code from my multi-threading example without actually working your way through the steps I outlined. The first step is to write a simple method that performs the action you want performed. If you'd done that I don't see that you could have ended up with that result. This is what so often happens when people copy and paste code without properly reading the instructions that go with it, which is why I prefer to avoid posting code a lot of the time.
Re: problem with thread safe changing controls-agian
busted. put in my deffence ive been staring at this screen for 9 hours. im a little punchie. im just having trouble understanding how it works.
Re: problem with thread safe changing controls-agian
Code:
Private Delegate Sub SetLabelTextInvoker(ByVal txt As String)
Private Sub SetLabelText(ByVal txt As String)
If Me.Label1.InvokeRequired Then
Me.Label1.Invoke(New SetLabelTextInvoker(AddressOf SetLabelText), txt)
Else
Me.Label1.Text = txt
End If
End Sub
Re: problem with thread safe changing controls-agian
I spent a lot of time and put a lot of effort into that thread. The second post addresses EXACTLY what you're asking here. If you'd simply read the thread and followed it STEP BY STEP you would have had your solution. I'm glad to see my efforts weren't wasted.
Re: problem with thread safe changing controls-agian
ah don't get pissed jmc, some people need it spelled out once or twice before they get it..
Re: problem with thread safe changing controls-agian
Quote:
Originally Posted by MaximilianMayrhofer
ah don't get pissed jmc, some people need it spelled out once or twice before they get it..
Noone needs something spelled out twice. They just need to read what's been spelled out the first time. If your first instinct is to copy the code and ignore the information then you simply will not learn. Some pasted code may or may not fix the immediate problem but reading a provided explanation that's intended to give you an understanding will actually teach you something.
Re: problem with thread safe changing controls-agian