Solved

How to launch form from form in new thread

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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