By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,166 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,166 IT Pros & Developers. It's quick & easy.

Adding controls to the form... help

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/de...ms06112002.asp

--
Rob Windsor
G6 Consulting
Toronto, Canada


"Arthur Dzhelali" <a.********@theday.com> wrote in message
news:Xn********************************@216.168.3. 44...
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

Nov 20 '05 #2

P: n/a
"Arthur Dzhelali" <a.********@theday.com> schrieb
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

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.
--
Armin

Nov 20 '05 #3

P: n/a
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.

"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in
news:en*************@TK2MSFTNGP12.phx.gbl:
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/de...y/en-us/dnform
s/html/winforms06112002.asp


Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.