Adding controls to the form... help

  • Thread starter Thread starter Arthur Dzhelali
  • Start date Start date
A

Arthur Dzhelali

I am trying dynamically create controls and add them to the form on the
Separate thread.
I know that I have to use delegate in order to update preexisting
controls on the form, but when I ma trying create new control I am
getting this message:


System.ArgumentException: Controls created on one thread cannot be
parented to a control on a different thread.


this is a sample code to reproduce error:

Delegate Sub MyDelegate()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dd As New MyDelegate(AddressOf addctrl)
For i As Int32 = 0 To 10
Try
dd.BeginInvoke(Nothing, Nothing)
Catch ex As Exception
End Try
System.Threading.Thread.Sleep(500)
Next
End Sub
Sub addctrl()
Try
Dim x As New TextBox
Me.Controls.Add(x)
Debug.WriteLine(Me.Controls.Count)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

End Sub

is there any way around it?

basicaly I have dataset which is updated and repopulated on the separate
thread and the it raises event and form gets recreated. IS there a way to
marshal event to proper thread??
any Ideas
 
Hi Arthur,

You need to use the form's Invoke method to switch you call back to the
form's thread. The code basically stays the same. NOTE: I'm writing this
code in Outlook so no sytax check has been done (hopefull that feature will
be in Office 2004!)

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dd As New MyDelegate(AddressOf addctrl)
For i As Int32 = 0 To 10
Me.Invoke(dd, Nothing)
Next
End Sub

For more information check out Chris Sell's series on Safe, Simple
Multithreading in Windows Forms.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp
 
Arthur Dzhelali said:
I am trying dynamically create controls and add them to the form on
the Separate thread.
I know that I have to use delegate in order to update preexisting
controls on the form, but when I ma trying create new control I am
getting this message:


System.ArgumentException: Controls created on one thread cannot be
parented to a control on a different thread.


this is a sample code to reproduce error:

Code:
is there any way around it?

basicaly I have dataset which is updated and repopulated on the
separate  thread and the it raises event and form gets recreated. IS
there a way to  marshal event to proper thread??
any Ideas[/QUOTE]


Where are the other threads? You must call the Invoke or BeginInvoke methods
of the _Control_ to call the procedure in the thread that created the
control. It does not help to call the BeginInvoke method of the _Delegate_.


In the other thread(s), and if the code is within the same Form:
Me.Invoke(New MyDelegate(AddressOf addctrl))

Note: In your example this does not make sense because there is only one
thread.
 
Thanks, I tryed this before it did not work.
I but I gave it one more try.
It works now.
It is hard to debug when you have several thread going on.

BTW, invoke doesnot produce async result, you have to use begin envoke in
order to create separate thread.
 
Back
Top