Results 1 to 8 of 8

Thread: problem with thread safe changing controls-agian

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Location
    Botswana
    Posts
    107

    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
    I am using Microsoft Visual Basic 2008 Express Edition. I use Access for my data base

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Location
    Botswana
    Posts
    107

    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.
    I am using Microsoft Visual Basic 2008 Express Edition. I use Access for my data base

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: problem with thread safe changing controls-agian

    Teach a man to fish

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width