Solved

How to launch form from form in new thread

Posted on 2007-04-11
5
367 Views
Last Modified: 2013-11-07
Hi experts,

I need to use a launcher form
which one can launch other forms
eg :

you have Mainform and form1, form2 and form3 in the solution

now with the mainform, i need to launch one of the 3 form in another thread and close himself.

so for the final result, i will have form1 or form2 or form3 launched without the MainForm

can you help me?

regards
0
Comment
Question by:Dnx_7
  • 3
5 Comments
 
LVL 5

Expert Comment

by:voordes
ID: 18893856
Hello,

In general it is is disadvised to create and run forms on any other thread then the main UI thread, because any other 'worker' thread does not have a messagepump and can therefore result in strange behaviour.
Nonetheless I have once experemented with the same thing and fot things working using the code below:

    Private Sub StartNewForm()
        'Setup Thread
        Dim newThread As New Threading.Thread(AddressOf RunNewForm)
        'Set the apartmentstate to STA so OLE functionality works and does not give errors
        newThread.SetApartmentState(Threading.ApartmentState.STA)
        'Start the thread
        newThread.Start()
    End Sub


    Private Sub RunNewForm()
        'Initialize a new form
        Dim newForm As New Form
        'Show the form modeless
        newForm.Show()
        'Start a new messagepump on this thread by using the application run method
        Application.Run()
    End Sub


SV
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18894107
What version VB.Net are you working in Dnx_7?
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 18894219
Idle_Mind,

Your attention is needed at:

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_22500674.html

Please respond to post starting with:

"I am not sure how to apply code from Idle_Mind:

Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Declare Function ScreenToClient Lib "user32" Alias "ScreenToClient" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

to my code:

Private Sub Form_Timer()"

Thank you.

Mike
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18895749
The "problem", as you see it, is that when you close the main form of an applicaton the whole application closes.  Thus your thinking that you need a new thread with a new message pump to keep the app alive.

A different approach is to not associate the message pump with a form, but instead with your own version of the ApplicationContext() class:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.applicationcontext(VS.71).aspx
This allows YOU to decide WHEN your application should exit on your own terms.

Here is how to do it in VB.Net 2003 with the app starting from Sub Main().  Just call "MC.SwitchTo()" and pass it the new form the app should stay open with:

Module Module1

    Public MC As MyContext

    Public Sub Main()
        MC = New MyContext(New Form1)
        Application.Run(mc)
    End Sub

    Public Class MyContext
        Inherits ApplicationContext

        Private WithEvents mainFrm As Form

        Public Sub New(ByVal frm As Form)
            SwitchTo(frm)
        End Sub

        Public Sub SwitchTo(ByVal frm As Form)
            Dim tmpFrm As Form = mainFrm
            mainFrm = frm
            If Not (tmpFrm Is Nothing) Then
                tmpFrm.Close()
            End If
            If Not (mainFrm Is Nothing) Then
                mainFrm.Show()
            End If
        End Sub

        Private Sub mainFrm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles mainFrm.Closed
            Application.ExitThread()
        End Sub

    End Class

End Module

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MC.SwitchTo(New Form2)
    End Sub

End Class
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18903196
Forgot to mention...

If you have VB.Net 2005, then go to Project --> Properties and change the "Shutdown Mode" dropdown to "When last form closes".  Then you can simply open the new form and close the old form and the app will remain open as long as you have ~some~ form still open.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

11 Experts available now in Live!

Get 1:1 Help Now