By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

Killing off a Descendant

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schrieb
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

Nov 20 '05 #2

P: n/a
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40*********************@news.freenet.de...
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schrieb
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.
Nov 20 '05 #3

P: n/a
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schrieb
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.
--
Armin

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.