On 2008-05-04, John <in**@nospam.infovis.co.ukwrote:
Hi
I am trying to create (not open) several forms in the background threads
using the code given below at the end.
1. Am I doing it correctly?
2. How can I get handle sot these forms in the calling sub so I can access
these forms after creation.
3. How do I know when threads have finished forms creation so I can show the
form if needed?
Thanks
Regards
Code
====
Private Sub Main()
Dim t1 As New Thread(AddressOf CreateForm)
t1.Start(GetType(Form1))
Dim t2 As New Thread(AddressOf CreateForm)
t2.Start(GetType(Form2))
Dim t3 As New Thread(AddressOf CreateForm)
t3.Start(GetType(Form3))
End Sub
Public Sub CreateForm(ByVal ft As Object)
Dim t As Type = TryCast(ft, Type)
If Not t Is Nothing Then
Using f As Form = TryCast(Activator.CreateInstance(t, False), Form)
' Add this to show the form
Application.Run(f)
End Using
End If
End Sub
Application.Run starts a new message pump on the current thread - and
shows the form. As for access to the forms after creation... Well, if
you intended to do that then you would need to increase the scope of you
form variables, and that would greatly increase the complexity of the
code. You would of course, have to make sure that all access to those
forms was marshaled correctly (using Invoke or BeginInvoke/EndInvoke) -
since as always, windows forms are NOT thread safe, and can not be
directly accessed from other threads.
Any logic on that thread would have to be in the form that is passed to
applciation.run, since it will not return until the form is closed - in
other words, it's a blocking method.
Can you explain exactly what you are trying to accomplish? There is
probably a simpler way of doing whatever it is your trying to do :)
--
Tom Shelton