Results 1 to 8 of 8

Thread: Cross-thread operation not valid

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Question Cross-thread operation not valid

    Hello,

    I'm having trouble trying to update the Form1's text, I've tried the next code, but it throws this exception:

    Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.

    What am I doing wrong?

    vb.net Code:
    1. Private Sub PercentCompleted(ByVal progress As Integer) Handles PercentDone.Progress
    2.         Dim deleg As New ProgressDone(AddressOf UpdateProgress)
    3.         Me.ProgressBar1.Invoke(deleg, progress)
    4.  
    5.     End Sub
    6.  
    7.     Private Sub UpdateProgress(ByVal e As Object)
    8.         Me.ProgressBar1.Value = CInt(e)
    9.         Me.Text = CStr(e) & "% Completed"
    10.     End Sub

    Thanks in advance!!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Cross-thread operation not valid

    try this:

    vb Code:
    1. Private Delegate Sub UpdateProgressCallback(ByVal e As Object)
    2.  
    3. Private Sub UpdateProgress(ByVal e As Object)
    4.     If ProgressBar1.InvokeRequired Then
    5.         ProgressBar1.Invoke(New UpdateProgressCallback(AddressOf UpdateProgress),e)
    6.     Else    
    7.         If me.InvokeRequired Then
    8.             me.Invoke(New UpdateProgressCallback(AddressOf UpdateProgress),e)
    9.         Else            
    10.             Me.ProgressBar1.Value = CInt(e)
    11.             Me.Text = CStr(e) & "% Completed"
    12.         end if
    13.     end if
    14. End Sub

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Cross-thread operation not valid

    Hey Paul,

    thanks for the fast reply. The progressbar invoke is working great, but it still gives me the Form1 error. (It does change Form1's text, but then it throws the error).

    I have no idea of what it could be.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Cross-thread operation not valid

    try invoking the form too

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Cross-thread operation not valid

    It's telling me to refer to it as Me, instead of Form1.

    I'm trying this:

    vb.net Code:
    1. Private Sub PercentCompleted(ByVal e As Integer) Handles PercentDone.Progress
    2.         If Me.ProgressBar1.InvokeRequired Then
    3.             Me.ProgressBar1.Invoke(New UpdateProgressCallBack(AddressOf PercentCompleted), e)
    4.         Else
    5.             If Me.InvokeRequired Then
    6.                 Me.Invoke(New UpdateProgressCallBack(AddressOf PercentCompleted), e)
    7.             Else
    8.                 If Form1.InvokeRequired Then
    9.                     Form1.Invoke(New UpdateProgressCallBack(AddressOf PercentCompleted), e)
    10.                 End If
    11.                 Me.ProgressBar1.Value = e
    12.                 Me.Text = CStr(e) & "% Completed"
    13.             End If
    14.         End If
    15.     End Sub
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  6. #6

    Re: Cross-thread operation not valid

    Why not use a Background Worker...or is that not possible? That would help handle any of your Cross-Threading problems, but I assume you've already tried that path?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Cross-thread operation not valid

    The reason why I'm not using a BW is because the process is being done by a class, so I had the class raise an event to report the progress and then I would report it the old-fashioned way.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  8. #8

    Re: Cross-thread operation not valid

    Oho, that explains why then. I have nothing else to contribute unfortunately .

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