Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VB Cross-Thread Operation not valid  (Read 9497 times)

0 Members and 1 Guest are viewing this topic.

alecsillidan

    Topic Starter


    Beginner

    VB Cross-Thread Operation not valid
    « on: July 21, 2012, 05:40:45 AM »
    Hello again!
    I encountered another strange thing while programming for fun, this is the sub where I get an error:
    Code: [Select]
        Private Sub checkversion()
            Me.VersionTableAdapter.Fill(Me.Update.version)
            onlineversion1 = ListBox1.SelectedItem("version").ToString()
            onlineversion = CDbl(onlineversion1)
            AccessControl()
            textonline.Text = "Versiune Online: " & onlineversion
            Label1.Text = onlineversion1
            If version = onlineversion Then

            Else
                actualizaredisp = 1
                MsgBox("A aparut o versiune mai noua! Apasati Ok pentru a face update." & vbLf & "Versiunea programului: " & version & vbLf & "Versiunea noua: " & onlineversion & vbLf & vbLf & "Apasati F1 pentru actualizare!", MsgBoxStyle.YesNo, "Actualizare disponibila!")
            End If
        End Sub

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

    Code: [Select]
    System.InvalidOperationException was unhandled by user code
      Message=Cross-thread operation not valid: Control 'pornire' accessed from a thread other than the thread it was created on.
      Source=System.Windows.Forms
      StackTrace:
           at System.Windows.Forms.Control.get_Handle()
           at System.Windows.Forms.Control.get_InternalHandle()
           at System.Windows.Forms.Control.get_CreateParams()
           at System.Windows.Forms.Label.get_CreateParams()
           at System.Windows.Forms.Control.SizeFromClientSize(Int32 width, Int32 height)
           at System.Windows.Forms.Control.SizeFromClientSize(Size clientSize)
           at System.Windows.Forms.Label.GetBordersAndPadding()
           at System.Windows.Forms.Label.GetPreferredSizeCore(Size proposedConstraints)
           at System.Windows.Forms.Control.GetPreferredSize(Size proposedSize)
           at System.Windows.Forms.Label.GetPreferredSize(Size proposedSize)
           at System.Windows.Forms.Control.get_PreferredSize()
           at System.Windows.Forms.Label.AdjustSize()
           at System.Windows.Forms.Label.OnTextChanged(EventArgs e)
           at System.Windows.Forms.Control.set_Text(String value)
           at System.Windows.Forms.Label.set_Text(String value)
           at centrala_concurs.pornire.Button10_Click() in C:\Users\alecs_000\Documents\Visual Studio 2010\Projects\centrala_concurs\centrala_concurs\pornire.vb:line 106
           at centrala_concurs.pornire.BackgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\alecs_000\Documents\Visual Studio 2010\Projects\centrala_concurs\centrala_concurs\pornire.vb:line 116
           at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
           at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
      InnerException:

    I searched on Google but didn't actually understand the code and I don't use pieces of code that I don't understand.

    Thank you again:D
    We never had to cheat 'cause we already won!

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VB Cross-Thread Operation not valid
    « Reply #1 on: July 21, 2012, 09:59:46 AM »
    When accessing Windows Forms from another thread, you need to use Invoke on the target control. eg. to set the text of a label from another thread:


    Code: [Select]
    Label1.Text="hello"


    will throw an error.

    Code: [Select]
    Me.Invoke((Function() Label1.Text="hello"))
    I'm not 100% Sure for VB; this is afaik the equivalent to this C#:
    Code: [Select]
    this.Invoke((MethodInvoker)(()=>Label1.Text="hello"));

    I was trying to dereference Null Pointers before it was cool.

    alecsillidan

      Topic Starter


      Beginner

      Re: VB Cross-Thread Operation not valid
      « Reply #2 on: July 21, 2012, 11:53:19 AM »
      Figured it out, works now:D
      now i'm getting another error:
      Code: [Select]
              If version = onlineversion Then

              Else
                  MsgBox("A aparut o versiune mai noua! Apasati Ok pentru a face update." & vbLf & "Versiunea programului: " & version & vbLf & "Versiunea noua: " & onlineversion & vbLf & vbLf & "Apasati F1 pentru actualizare!", MsgBoxStyle.YesNo, "Actualizare disponibila!")
                  actualizare.Show()
              End If
      An error occurred creating the form. See Exception.InnerException for details.  The error is: Conversion from string "" to type 'Double' is not valid.

      Visual Studio shows that the error is at actualizare.show()
      We never had to cheat 'cause we already won!

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: VB Cross-Thread Operation not valid
      « Reply #3 on: July 21, 2012, 11:57:55 AM »
      It works setting the label text, the error occures in line "If version = onlineversion Then" and I'm not getting the ideea...how i'm supposed to make the if condition if vb can't read those variables or idk why i'm getting that error:|

      The exception message indicates the error is caused by attempting to set the text of the label:

      Quote
      StackTrace:
             at System.Windows.Forms.Control.get_Handle()
             at System.Windows.Forms.Control.get_Intern alHandle()
             at System.Windows.Forms.Control.get_Create Params()
             at System.Windows.Forms.Label.get_CreatePa rams()
             at System.Windows.Forms.Control.SizeFromCl ientSize(Int32 width, Int32 height)
             at System.Windows.Forms.Control.SizeFromCl ientSize(Size clientSize)
             at System.Windows.Forms.Label.GetBordersAn dPadding()
             at System.Windows.Forms.Label.GetPreferred SizeCore(Size proposedConstraints)
             at System.Windows.Forms.Control.GetPreferr edSize(Size proposedSize)
             at System.Windows.Forms.Label.GetPreferred Size(Size proposedSize)
             at System.Windows.Forms.Control.get_Prefer redSize()
             at System.Windows.Forms.Label.AdjustSize()
             at System.Windows.Forms.Label.OnTextChange d(EventArgs e)
             at System.Windows.Forms.Control.set_Text(String value)
             at System.Windows.Forms.Label.set_Text(String value)

             at centrala_concurs.pornire.Button10_Click() in C:\Users\alecs_000\Documents\Visual Studio 2010\Projects\centrala_concurs\centrala_concurs\pornire.vb:line 106
             at centrala_concurs.pornire.BackgroundWork er1_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\alecs_000\Documents\Visual Studio 2010\Projects\centrala_concurs\centrala_concurs\pornire.vb:line 116
             at System.ComponentModel.BackgroundWorker. OnDoWork(DoWorkEventArgs e)
             at System.ComponentModel.BackgroundWorker. WorkerThreadStart(Object argument)

      What is line 106 and 116 of "pornire.vb"?
      I was trying to dereference Null Pointers before it was cool.

      alecsillidan

        Topic Starter


        Beginner

        Re: VB Cross-Thread Operation not valid
        « Reply #4 on: July 21, 2012, 11:58:48 AM »
        nvm i'm noob, i modified the previous post, read it again please:))
        We never had to cheat 'cause we already won!

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: VB Cross-Thread Operation not valid
        « Reply #5 on: July 21, 2012, 12:04:12 PM »
        See Exception.InnerException for details.

        Sounds like an error within the Form_Load to me, but without the InnerException it's hard to say exactly where it is.

        EDIT: if you are using Visual Studio, the "Exception Helper" should let you look at the inner exception.
        I was trying to dereference Null Pointers before it was cool.

        alecsillidan

          Topic Starter


          Beginner

          Re: VB Cross-Thread Operation not valid
          « Reply #6 on: July 21, 2012, 12:16:17 PM »
          It's working! I was using a double variable with a string text =))
          Thank you dude, you're life saver :D
          We never had to cheat 'cause we already won!