• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

How to launch form from form in new thread

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
Dnx_7
Asked:
Dnx_7
  • 3
1 Solution
 
voordesCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.Net are you working in Dnx_7?
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now