Killing off a Descendant

P

Phill. W

Here's another little challenge.

I have a [base] Form from which lots of others will be derived.
One of the base Form properties is a communication "channel"
to an external application. If this external application isn't running,
I want to /prevent/ the creation of the derived Form (essentially
kill the [derived] application stone dead).

I tried throwing an Exception, but being in the Form constructors,
there's nowhere I can easily catch it again, so I get the JIT Debugger
dialog - presumably my users would get something similarly unpleasant.
I even tried (gasp) Application.Exit() but that doesn't seem to work as
I thought it would - given:

[base Form class]

#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Try
ConnectToExternalApp()
Catch ex as MyException
Me.Close()
Me.Dispose()
Application.Exit()
End Try

End Sub

Even with the above, execution resumes in the Sub New() of the
derived Form, although that immediately bombs out with an
ObjectDisposedException - which I can't seem to catch.

Any Suggestions?

TIA,
Phill W.
 
A

Armin Zingler

Phill. W said:
Here's another little challenge.

I have a [base] Form from which lots of others will be derived.
One of the base Form properties is a communication "channel"
to an external application. If this external application isn't
running, I want to /prevent/ the creation of the derived Form
(essentially kill the [derived] application stone dead).

I tried throwing an Exception, but being in the Form constructors,
there's nowhere I can easily catch it again, so I get the JIT
Debugger dialog - presumably my users would get something similarly
unpleasant. I even tried (gasp) Application.Exit() but that doesn't
seem to work as I thought it would - given:

[base Form class]

#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Try
ConnectToExternalApp()
Catch ex as MyException
Me.Close()
Me.Dispose()
Application.Exit()
End Try

End Sub

Even with the above, execution resumes in the Sub New() of the
derived Form, although that immediately bombs out with an
ObjectDisposedException - which I can't seem to catch.

Any Suggestions?

Don't catch the exception in the constructor, so you'll get it where you
create the instance:

dim f as YourForm
try
f = new YourForm
catch
end try


I'd probably do the "ConnectToExternalApp" outside the Form and only create
an instance and pass the necessary data to the Form if ConnectToExternalApp
was successful. This can be put in a Shared Function within the Form class,
and the Function returns a new instance.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
P

Phill. W

Armin Zingler said:
Phill. W said:
Here's another little challenge.

I have a [base] Form from which lots of others will be derived.
One of the base Form properties is a communication "channel"
to an external application. If this external application isn't
running, I want to /prevent/ the creation of the derived Form
(essentially kill the [derived] application stone dead).
.. . .
Don't catch the exception in the constructor, so you'll get it where
you create the instance:

Ah! I don't (yet). The inherited form is the "main" (and usually only)
form in the application, and VB is currently starting it for me
- presumably, I'm going to have to change this and insist that my
Developers use Sub Main to get all their programs up and running?

Regards,
Phill W.
 
A

Armin Zingler

Phill. W said:
I have a [base] Form from which lots of others will be
derived. One of the base Form properties is a communication
"channel" to an external application. If this external
application isn't running, I want to /prevent/ the creation of
the derived Form (essentially kill the [derived] application
stone dead).
. . .
Don't catch the exception in the constructor, so you'll get it
where you create the instance:

Ah! I don't (yet). The inherited form is the "main" (and usually
only) form in the application, and VB is currently starting it for
me - presumably, I'm going to have to change this and insist that
my Developers use Sub Main to get all their programs up and
running?

If you want to check something /before/ creating the main Form, yes, you'd
have to write a sub main. That's what I would do, but others will perhaps
make other suggestions. I don't like interrupting creating/showing a Form,
and I also prefer to use a Form only as the user interface and do jobs like
'ConnectToExternalApp' outside the Form, at least if both reasons come
together.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top