Solved

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

Posted on 2006-11-09
15
852 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Dnx_7
  • 4
  • 3
  • 2
  • +5
15 Comments
 
LVL 10

Assisted Solution

by:gangwisch
gangwisch earned 25 total points
ID: 17905706
can't you use me.parentform to reference that form
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17905715
Are you using VS2005 ? if you are you cannot do this.
The second form will run on the main forms thread.
0
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17905781
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
 
LVL 2

Author Comment

by:Dnx_7
ID: 17905802
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
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 25 total points
ID: 17905856
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
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 150 total points
ID: 17905955
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17906895
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Accepted Solution

by:
riyazthad earned 200 total points
ID: 17909100
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17909343
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
 
LVL 6

Expert Comment

by:riyazthad
ID: 17909421
Did you add any other code or anything doing in close event?

That peice of code is working fine in my end.

Thad
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 17909501
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
 
LVL 6

Expert Comment

by:riyazthad
ID: 17909874
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17909981
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
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 150 total points
ID: 17912119
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
 
LVL 2

Author Comment

by:Dnx_7
ID: 17925700
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now