1.

Solve : VB Cross-Thread Operation not valid?

Answer»

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:DWhen 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"));

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()Quote from: alecsillidan on July 21, 2012, 11:53:19 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"?
nvm i'm noob, i modified the previous POST, read it again please:))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.It's working! I was using a double variable with a string text =))
Thank you dude, you're life saver


Discussion

No Comment Found