Link to home
Start Free TrialLog in
Avatar of Dnx_7
Dnx_7Flag for Belgium

asked on

How to open form in a new thread an montor it in the main form

hi experts,

all is in the title!

i have a mainform
i would like to launch a form from the main form in a new thread and monitor it
eg :

report status, thread status, exchange information between the main form and the form in the new thread

is it possible? if yes how?

regards
SOLUTION
Avatar of gangwisch
gangwisch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kinger247
Kinger247

Are you using VS2005 ? if you are you cannot do this.
The second form will run on the main forms thread.
If you were to launch a new thread, and in that first sub you declared a new form and set it to show... would that still run on the main thread?

I've made progress forms to show on seperate threads to keep track of work being done before, but I always just assumed it was working on the thread I had intended it to?
Avatar of Dnx_7

ASKER

yes i'm using visual 2005

me.parentform need mdi form right? or i'm wrong?

i guess it is possible because i had made a "sample" but that is not working properly

i start a new thread and invoke a new form
then i add the process into an arraylist to keep track of the new form

then a timer will cast the arraylist to the form and then retrieve information

SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do everything from the main UI thread.  To "create" the form in a different thread you would need to use Delegates and Invoke() to marshal the call from your thread to the main UI thread where it is safe to create controls/forms...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
riyazthad,

Using your code:

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim t As New System.Threading.Thread(AddressOf CallMe)
            t.Start()
        End Sub

        Private Sub CallMe()
            Dim f As New Form2
            System.Windows.Forms.Application.Run(f)
        End Sub

    End Class

Click the button to open up Form2.  Now close Form2...then close Form1.  The app gets stuck and doesn't exit completely...

Can you explain or fix that?
Did you add any other code or anything doing in close event?

That peice of code is working fine in my end.

Thad
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes in 2003, you can do it. It wont make any issues. If you are using VS 2005, it is good to call using delegate and Invoke.

Right...2003 doesn't throw an error. Cross thread communication without Delegates and Invoke() is a bad practice...

Just as long as the author is aware of that.  =)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dnx_7

ASKER

hi, i'm aware about the cross invalid thread and i have to make this property to true
that's why i'm asking people experts here to help me to create form form the main ui form in safe thread...

regards