Dnx_7
asked on
Multithreading, how to access property on the new form
Hi experts
i have a main form that launch many other forms
so i do that :
on click button to launch a new form
Dim nThread As New Thread(AddressOf newThread)
nThread.Name = Me.txtBxComputerName.Text & "_" & Me.TreeView.SelectedNode.T ext
MyThreads.Add(nThread) 'arraylist
nThread.Start()
the sub :
Private Sub newThread()
Dim frm As New MainReader 'the form to load
frm.setComputerName = Me.txtBxComputerName.Text. Trim
frm.setSelectedQueue = Me.TreeView.SelectedNode.T ext
Try
Application.Run(frm)
Catch ex As System.InvalidOperationExc eption
' description :A main message loop is already running on this thread.
MsgBox("Cannot open the reader : " & ex.Message)
End Try
End Sub
i have an exception on these lines :
frm.setComputerName = Me.txtBxComputerName.Text. Trim
frm.setSelectedQueue = Me.TreeView.SelectedNode.T ext
illegal cross...
so i set Control.CheckForIllegalCro ssThreadCa lls on False
but i'm sure that theres is a better solution to access property of a new form
can you help me please?
kind regards
i have a main form that launch many other forms
so i do that :
on click button to launch a new form
Dim nThread As New Thread(AddressOf newThread)
nThread.Name = Me.txtBxComputerName.Text & "_" & Me.TreeView.SelectedNode.T
MyThreads.Add(nThread) 'arraylist
nThread.Start()
the sub :
Private Sub newThread()
Dim frm As New MainReader 'the form to load
frm.setComputerName = Me.txtBxComputerName.Text.
frm.setSelectedQueue = Me.TreeView.SelectedNode.T
Try
Application.Run(frm)
Catch ex As System.InvalidOperationExc
' description :A main message loop is already running on this thread.
MsgBox("Cannot open the reader : " & ex.Message)
End Try
End Sub
i have an exception on these lines :
frm.setComputerName = Me.txtBxComputerName.Text.
frm.setSelectedQueue = Me.TreeView.SelectedNode.T
illegal cross...
so i set Control.CheckForIllegalCro
but i'm sure that theres is a better solution to access property of a new form
can you help me please?
kind regards
ASKER
System.InvalidOperationExc eption was unhandled
Message="Cross-thread operation not valid: Control 'TreeView' accessed from a thread other than the thread it was created on."
Source="System.Windows.For ms"
StackTrace:
at System.Windows.Forms.Contr ol.get_Han dle()
at System.Windows.Forms.Contr ol.SendMes sage(Int32 msg, Int32 wparam, Int32 lparam)
at System.Windows.Forms.TreeV iew.get_Se lectedNode ()
at AutomationReader.frmMain.n ewThread() in C:\Projet .NET\AutomationReader\Auto mationRead er\frmMain .vb:line 191
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.ru nTryCode(O bject userData)
at System.Runtime.CompilerSer vices.Runt imeHelpers .ExecuteCo deWithGuar anteedClea nup(TryCod e code, CleanupCode backoutCode, Object userData)
at System.Threading.Execution Context.Ru nInternal( ExecutionC ontext executionContext, ContextCallback callback, Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
regards
Message="Cross-thread operation not valid: Control 'TreeView' accessed from a thread other than the thread it was created on."
Source="System.Windows.For
StackTrace:
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.TreeV
at AutomationReader.frmMain.n
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Runtime.CompilerSer
at System.Threading.Execution
at System.Threading.Execution
at System.Threading.ThreadHel
regards
Hi Dnx_7
you can't access controls like this from a new thread, the Controls are in a separate UI thread and they are not directly accessible from outside the Thread boundary
you need to pass arguments to your Thread Function so that the Properties can be set
Try something like this
Public Class ThreadData
Public ComputerName As String
Public SelectedQueue As String
Public Sub newThread()
Dim frm As New MainReader
frm.setComputerName = ComputerName
frm.setSelectedQueue = SelectedQueue
Try
Application.Run(frm)
Catch ex As System.InvalidOperationExc eption
' description :A main message loop is already running on this thread.
MsgBox("Cannot open the reader : " & ex.Message)
End Try
End Sub
End Class
Then to call it
Dim ThData As New ThreadData
ThData.ComputerName = Me.txtBxComputerName.Text. Trim
ThData.setSelectedQueue = Me.TreeView.SelectedNode.T ext
Dim nThread As New Thread(AddressOf ThData.newThread)
nThread.Name = Me.txtBxComputerName.Text & "_" & Me.TreeView.SelectedNode.T ext
MyThreads.Add(nThread)
nThread.Start()
#I have typed out the code here so take care of some Typo or some silly mistake :-)
you can't access controls like this from a new thread, the Controls are in a separate UI thread and they are not directly accessible from outside the Thread boundary
you need to pass arguments to your Thread Function so that the Properties can be set
Try something like this
Public Class ThreadData
Public ComputerName As String
Public SelectedQueue As String
Public Sub newThread()
Dim frm As New MainReader
frm.setComputerName = ComputerName
frm.setSelectedQueue = SelectedQueue
Try
Application.Run(frm)
Catch ex As System.InvalidOperationExc
' description :A main message loop is already running on this thread.
MsgBox("Cannot open the reader : " & ex.Message)
End Try
End Sub
End Class
Then to call it
Dim ThData As New ThreadData
ThData.ComputerName = Me.txtBxComputerName.Text.
ThData.setSelectedQueue = Me.TreeView.SelectedNode.T
Dim nThread As New Thread(AddressOf ThData.newThread)
nThread.Name = Me.txtBxComputerName.Text & "_" & Me.TreeView.SelectedNode.T
MyThreads.Add(nThread)
nThread.Start()
#I have typed out the code here so take care of some Typo or some silly mistake :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My two cents worth? 20/80 split between Nanda and Arif.
Can u post me the complete error message with stack trace
Thanks,
Nanda