Close Form

  • Thread starter Thread starter Stephan Moeller
  • Start date Start date
S

Stephan Moeller

Hi,
I want to stop my Forms-Application from the constructor
of the form-class.
I try to make some connections in the constructor. If
there are some erros
I want to stop the application.
Now I tried a few thinks:

this.Close();

which has no effect, because the Form is not open in the
constructor.
The form opens.



Application.Exit();

has also no effect. The Form still opens.



System.Threading.Thread.CurrentThread.Abort();

throws an exception which I can´t catch.


How can I stop my program in the constructor without any
errors ?

Thanx and regards

Stephan
 
Hi,

I used the Application.Run method to launch my form from a
Main method, rather than launching the form directly.
This allows me to have initialisation take place after the
constuctor while being able to handle the exceptions
gracefully. I don't like having too many potential
exception causing functions being called in my
constructors. The Run method will take control of the
form afterwards and it will behave as if it was launched
directly, unless there is an unhandled exception, in which
case the catch statement below will actually catch them
(which is an added bonus).

Note I am still using Framework 1.0 but it should be
similar in 1.1 (I believe the STAThread attribute has
changed)

Gavin

This is an example of my code.

<STAThread()> _
Public Shared Sub Main()

Try
Dim myForm As MyFormClass

' Call your constructor here and if it
' fails exceptions can be handled below.
myForm = New MyFormClass()

' If the initialisation is going to
' take some time showing the form gives
' the appearance that something is happening
myForm.Show()

' I would recommend the real
' initialisation here.

Application.Run(myForm)

Catch oEx As System.Exception
MsgBox(oEx.Message)
Finally
' Tidy up etc
End Try
End Sub
 
Process myProcess = Process.GetCurrentProcess;
myProcess.Kill();

This will ungracefully shut down the process.

Hi,
I want to stop my Forms-Application from the constructor
of the form-class.
I try to make some connections in the constructor. If
there are some erros
I want to stop the application.
Now I tried a few thinks:

this.Close();

which has no effect, because the Form is not open in the
constructor.
The form opens.



Application.Exit();

has also no effect. The Form still opens.



System.Threading.Thread.CurrentThread.Abort();

throws an exception which I can´t catch.


How can I stop my program in the constructor without any
errors ?

Thanx and regards

Stephan
 
Would recommend your form throw an exception in the
constructor on the error condition....then you can handle
the exception as you wish...either present an appropriate
message, or just let the app end.

Good luck.
 
Back
Top