Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have a continuous cycle, which creates the controls.
The panel is being InvokeRequired = true and the object.
how to proceed in this case.
While true
 sleep(2000)
 dim myType as myType
 sTh = New Thread(New ParameterizedThreadStart(AddressOf AddMoreControls))
 sTh.Start(myType)
Wend
 
  
 Delegate Sub sAddControl(ByVal pParam() As Object)
 Private Sub ssAddControl(ByVal pParam() As Object)
	 Try
		 Dim pn As Panel = CType(pParam(0), Panel)
		 Dim ctl As MyControl = CType(pParam(1), MyControl)
		 pn.Controls.Add(ctl)
	 Catch ex As Exception
		Throw ex
	 End Try
 End Sub
 
 Private Sub AddMoreControls(byval myType as myType)
	 try
		 Dim ctl As New MyControl
		 ctl.Name = "txt_1"
		 ctl.myType = myType
 
		 if GroupBox.InvokeRequired then
 
		 Dim pParam(1) As Object
			 pParam(0) = myPanel
			 pParam(1) = ctl
			 Dim d As New sAddControl(AddressOf ssAddControl)
			 GroupBox.Invoke(d, New Object() {pParam})
		 else
			myPanel.add(ctl)
		 end if 
	Catch ex As Exception
		msgbox (ex)
	End Try
 end sub
 
[Edit]Code block added[/Edit]
Posted 30-Nov-12 3:53am
Edited 30-Nov-12 7:04am
ProgramFOX132.3K
v2
Comments
Sergey Alexandrovich Kryukov at 5-Feb-13 0:17am
   
How come you post non-answer (a comment) as "solution", and even accepted it formally?!
Don't do it, please. This is considered as abuse, and some even lost their memberships as a result of abuse report.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You really can't do any of this on a background thread. All controls MUST be created on the UI thread or you'll run into all kinds of problems that are extremely difficult to debug and can't fix.
 
You can still do this from a background thread, but you must Invoke a method on the UI thread to create the controls. You cannot touch any control or control container (like adding controls to a form, panel, group box, ...) from anything other than the UI thread.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

the AddMoreControls runs in a new Thread, the control (ctl) created in this function will not be in the same thread as the Panel (the UI Thread).
That's why I think you have the invalid operation.
You should include the creation of the control in the Invoked method.
Delegate Sub sAddMoreControl(ByVal myType as myType)
Private Sub AddMoreControls(byval myType as myType)
try
  if GroupBox.InvokeRequired then
    Dim d As New sAddMoreControl(AddressOf AddMoreControls)
    GroupBox.Invoke(d, New Object() {myType})
  else
    Dim ctl As New MyControl
    ctl.Name = "txt_1"
    ctl.myType = myType
    myPanel.add(ctl)
  end if
Catch ex As Exception
  msgbox (ex)
End Try
end sub 
 
Sorry if I made some syntax error in VB.NET, I use C# syntax more.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 279
1 OriginalGriff 190
2 DamithSL 185
3 Peter Leow 105
4 Kornfeld Eliyahu Peter 105
0 OriginalGriff 7,355
1 DamithSL 5,254
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100