Solved

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

Posted on 2006-11-09
15
859 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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 63

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 86

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
 
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 86

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 86

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

617 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