Forms, primary thread closing when closing startup form 
Author Message
 Forms, primary thread closing when closing startup form

Hello,

When you make a project that consists of two forms, of which form1 is the
startup form. When you run the application and then code the following under
a button under form1:

form2.show
form1.close

The program closes down, I think this is because the form1 runs in the
primary thread , and when it is closed , the primary thread end and all
'secondary' once ( like the one with the form2 ) end too.

You could solve this in multiple ways, like setting form2 as your startup
form and keep it hidden, or to keep form1 in memory by hiding it instead of
closing. But these are all ways round the problem.

So, what I would like is a clean solution which faces the problem as it is.

Can anyone help me ?
Can anyone validate my thread explaination ?

Thx in Advance,

-----------

Tom Deseyn



Sun, 28 Nov 2004 23:16:56 GMT  
 Forms, primary thread closing when closing startup form
You are very close on your thread thoughts, but it is not a threading issue.
The issue is that if you start your application by pointing to a form as the
startup object, then the message pump is totally handled by that form.  You
close the form , the message pump goes away.  No message pump and the rest
of the application fails.  The recommended way to handle this is to create a
module with the following in it.

Sub Main
    Application.Run(form1)
end Sub

and set that as the startup object.

HTH


Quote:
> Hello,

> When you make a project that consists of two forms, of which form1 is the
> startup form. When you run the application and then code the following
under
> a button under form1:

> form2.show
> form1.close

> The program closes down, I think this is because the form1 runs in the
> primary thread , and when it is closed , the primary thread end and all
> 'secondary' once ( like the one with the form2 ) end too.

> You could solve this in multiple ways, like setting form2 as your startup
> form and keep it hidden, or to keep form1 in memory by hiding it instead
of
> closing. But these are all ways round the problem.

> So, what I would like is a clean solution which faces the problem as it
is.

> Can anyone help me ?
> Can anyone validate my thread explaination ?

> Thx in Advance,

> -----------

> Tom Deseyn



Mon, 29 Nov 2004 00:49:22 GMT  
 Forms, primary thread closing when closing startup form
But that doesn't work either. I tried several versions of the code
(declaring form1 and form2 in the module or not in various combinations) and
ended up with this sub main (as the startup object):

Module Module1

Public Myform2 As New Form2()

Sub main()

Application.Run(New Form1()) ; Why do I have a New here and your example
doesn't?

End Sub

End Module

Form1 runs and a button on form1 shows form2. But closing form1 still kills
the app even though form2 is still running and is not even declared by
form1.

I tried a do loop (doing nothing) after the application.run in main, and
that is even odder. Closing form1 still kills form2, and the app continues
to run doing nothing.

No, now I am thoroughly confused.


Quote:
> You are very close on your thread thoughts, but it is not a threading
issue.
> The issue is that if you start your application by pointing to a form as
the
> startup object, then the message pump is totally handled by that form.
You
> close the form , the message pump goes away.  No message pump and the rest
> of the application fails.  The recommended way to handle this is to create
a
> module with the following in it.

> Sub Main
>     Application.Run(form1)
> end Sub

> and set that as the startup object.

> HTH



> > Hello,

> > When you make a project that consists of two forms, of which form1 is
the
> > startup form. When you run the application and then code the following
> under
> > a button under form1:

> > form2.show
> > form1.close

> > The program closes down, I think this is because the form1 runs in the
> > primary thread , and when it is closed , the primary thread end and all
> > 'secondary' once ( like the one with the form2 ) end too.

> > You could solve this in multiple ways, like setting form2 as your
startup
> > form and keep it hidden, or to keep form1 in memory by hiding it instead
> of
> > closing. But these are all ways round the problem.

> > So, what I would like is a clean solution which faces the problem as it
> is.

> > Can anyone help me ?
> > Can anyone validate my thread explaination ?

> > Thx in Advance,

> > -----------

> > Tom Deseyn



Mon, 29 Nov 2004 20:22:09 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Form Close Button Does Not Close the Form

2. Open form, close form, open form = disposed object exception

3. Prevent user from closing form using caption close button

4. Closing a form and closing Word

5. Tip : VB.NET Form Won't Close on Me.Close + User Controls

6. Form does not close from the UpperRight X Close button

7. Do I Find or Write the new Form Events (Activated,DeActivated,Closing,Closed)

8. problems using close [x] button when closing forms.

9. window.close closes webbrowser but not the form itself

10. How to prevent user closing a mdi form using the close control button

11. window.close closes Webbrowser but not the form itself

12. Do not close form on clicking close button in Visual Basic

 

 
Powered by phpBB® Forum Software