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
LVL 2
Dnx_7Asked:
Who is Participating?
 
riyazthadConnect With a Mentor Commented:
hi,

        Dim t As System.Threading.Thread
        t = New Threading.Thread(AddressOf CallMe)
        t.Start()


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

All code are in Form1 and you are calling Form2 in different thread from Form1.

Thad
0
 
gangwischConnect With a Mentor Commented:
can't you use me.parentform to reference that form
0
 
Kinger247Commented:
Are you using VS2005 ? if you are you cannot do this.
The second form will run on the main forms thread.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
DjDezmondCommented:
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?
0
 
Dnx_7Author Commented:
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

0
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi Dnx_7;

Me.Parent -> Gets or sets the parent container of the control. So a form can be a parent to another form and not be an MDI.

Me.MdiParent -> Gets or sets the current multiple document interface (MDI) parent form of this form.

All User Interface, UI, are created on the same thread.

Fernando
0
 
AlexFMConnect With a Mentor Commented:
To run form in a worker thread, you need to create message loop in this thread. By default, every thread doesn't have message loop. To start message loop, use Application.Run function:
Application.Run() - start message loop. Form can be created later.
Application.Run (Form) - start message loop and show form.

However, it is much better to create all UI elements in the main thread, and use worker threads only for background tasks like calculations, connectivity, hardware access etc.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
riyazthadCommented:
Did you add any other code or anything doing in close event?

That peice of code is working fine in my end.

Thad
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Sorry...my fault...it wasn't closing due to code I had in the form.

Despite that, the author said:

    "exchange information between the main form and the form in the new thread"

To SAFELY communicate between the two forms you would need Delegates and Invoke().

As demonstrated by this code in VB.Net 2005 which generates a "cross-thread" error:

Public Class Form1

    Private f As Form

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

    Private Sub CallMe()
        If f Is Nothing Then
            f = New Form
            System.Windows.Forms.Application.Run(f)
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Not (f Is Nothing) Then
            f.Text = "hello"     ' <---- cross thread error
        End If
    End Sub

End Class

The error clearly indicates that this is something you shouldn't be doing.  Since Delegates/Invoke() are necessary anyways, you should just run all forms on the main UI.

We should be putting PROCESSING, not Forms on seperate threads...
0
 
riyazthadCommented:
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.

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.  =)
0
 
AlexFMConnect With a Mentor Commented:
Cross-thread calls without Invoke:
VS2003 doesn't throw error, but results can be unpredictable. VS2005 throws exception under debugger, and gives unpredictable results without debugger.
It is strongly recommended to add the following line to program initialization code:

Control.CheckForIllegalCrossThreadCalls = true

In this case every illegal cross-thread call throws exception in any case, making program behavior 100% consistent.
0
 
Dnx_7Author Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.