how to acess Control Reference in Thread Method
-
I hv to get control reference in thread method let suppose in this example i m setting text lable of text control but it gives exception at Statment TextBox1.Text="DFD" Exception is Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on. code is below
Public Class data
Private myUtil As Util = New Util()
Private strSQL As String = ""
Public Sub savedata()
'Label1.Text = "DFDF"
' Dim d As data
TextBox1.Text = "DFDF"
Try
strSQL = "Insert into tbldata(pid,pname,pdesc) values(2, 'abc', 'description1')"
If (myUtil.ExecuteDBCommand(strSQL) = 1) Then
'MsgBox("Account(s) Assigned Sucessfully!")
Else
MessageBox.Show("Could not Save Record!", "Account Information!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End IfCatch ex As Exception End Try End Sub Private Sub data\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim t1 As Threading.Thread = New Threading.Thread(AddressOf savedata) t1.Start() End Sub
End Class
plz help
-
I hv to get control reference in thread method let suppose in this example i m setting text lable of text control but it gives exception at Statment TextBox1.Text="DFD" Exception is Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on. code is below
Public Class data
Private myUtil As Util = New Util()
Private strSQL As String = ""
Public Sub savedata()
'Label1.Text = "DFDF"
' Dim d As data
TextBox1.Text = "DFDF"
Try
strSQL = "Insert into tbldata(pid,pname,pdesc) values(2, 'abc', 'description1')"
If (myUtil.ExecuteDBCommand(strSQL) = 1) Then
'MsgBox("Account(s) Assigned Sucessfully!")
Else
MessageBox.Show("Could not Save Record!", "Account Information!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End IfCatch ex As Exception End Try End Sub Private Sub data\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim t1 As Threading.Thread = New Threading.Thread(AddressOf savedata) t1.Start() End Sub
End Class
plz help
Short version: any time you access a control you need to do it on the thread that it was created on. The Control class has
InvokeRequired
,BeginInvoke
andInvoke
properties to allow you to accomplish this. Long Version: http://www.perceler.com/articles1.php?art=crossthreads1[^] Longer Version: What's up with BeginInvoke?[^]:badger:
-
Short version: any time you access a control you need to do it on the thread that it was created on. The Control class has
InvokeRequired
,BeginInvoke
andInvoke
properties to allow you to accomplish this. Long Version: http://www.perceler.com/articles1.php?art=crossthreads1[^] Longer Version: What's up with BeginInvoke?[^]:badger:
great reference! :-D
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
great reference! :-D
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]