Solved

How to launch form from form in new thread

Posted on 2007-04-11
5
369 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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