Solved

How to launch form from form in new thread

Posted on 2007-04-11
5
371 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
[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
  • 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 86

Expert Comment

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

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 86

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 86

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrade code from VS 2010 to VS 2015 7 35
asp.net figure out error 3 26
Need help with another query 10 40
Check only one toolstripmenu item 12 32
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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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