Terminating application during class initialization

  • Thread starter Thread starter Wysiwyg
  • Start date Start date
W

Wysiwyg

I'm new to C# and figuring out how to program a third-party addon for an
application. I don't have a form class per-se but I initialize a class that
uses the existing application's user interface libraries to draw a form;
it's loaded from XML in this case.

My question is: how do I terminate the application completely when
initializing the instantiated class if an error occurs; i.e. can't connect.

So, my startup object is a static main which looks like so:

[STAThread]
static void Main(string[] args)
{
// Creating a vids object for the user interface (this is NOT a forms
class)
Vids oVids = new VIDS.Vids(args[0]);
// Start message loop
System.Windows.Forms.Application.Run();
}

Note that "Vids" is a class that does NOT inherit from
System.Windows.Forms.Form. Since it isn't a form I can't use "Run(new
VIDS.Vids(args[0]))" since the instantiated class doesn't inherit the form
class. I want to be able to terminate the app BEFORE starting the message
loop on the thread.

The constructor for the "Vids" class looks like so:

public Vids(string strCommandLine)
{
try
{
// Initialize connections, etc here
...
}
catch
{
// The initialization failed
System.Windows.Forms.MessageBox.Show("Application not found");
System.Windows.Forms.Application.Exit();
}
}

The class is initialized prior to the event processing being started with
"System.Windows.Forms.Application.Run();" in my main class. That means the
application thread starts even without the Vids class running.

How should I start the application if I want to be able to terminate it if
an error occurs? Should I set a public value in Vids to flag that the app
should terminate? Is there a command I can use to terminate the entire
process? ExitThread won't do anything for me in the constructor. Should I
move the actual initialization out of the constructor? In Visual Basic I'd
believe I'd just have a "Public Sub New()" subroutine for the initialization
and terminate with an "End" statement if I wanted to stop processing.

Thanks for any help!
Bill
 
Thanks, that's what I was looking for!

Jared Parsons said:
If you want to kill the process no matter what try Environment.Exit()

--
Jared Parsons [MSFT]
(e-mail address removed)
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
Wysiwyg said:
I'm new to C# and figuring out how to program a third-party addon for an
application. I don't have a form class per-se but I initialize a class
that
uses the existing application's user interface libraries to draw a form;
it's loaded from XML in this case.

My question is: how do I terminate the application completely when
initializing the instantiated class if an error occurs; i.e. can't
connect.

So, my startup object is a static main which looks like so:

[STAThread]
static void Main(string[] args)
{
// Creating a vids object for the user interface (this is NOT a forms
class)
Vids oVids = new VIDS.Vids(args[0]);
// Start message loop
System.Windows.Forms.Application.Run();
}

Note that "Vids" is a class that does NOT inherit from
System.Windows.Forms.Form. Since it isn't a form I can't use "Run(new
VIDS.Vids(args[0]))" since the instantiated class doesn't inherit the form
class. I want to be able to terminate the app BEFORE starting the message
loop on the thread.

The constructor for the "Vids" class looks like so:

public Vids(string strCommandLine)
{
try
{
// Initialize connections, etc here
...
}
catch
{
// The initialization failed
System.Windows.Forms.MessageBox.Show("Application not found");
System.Windows.Forms.Application.Exit();
}
}

The class is initialized prior to the event processing being started with
"System.Windows.Forms.Application.Run();" in my main class. That means the
application thread starts even without the Vids class running.

How should I start the application if I want to be able to terminate it if
an error occurs? Should I set a public value in Vids to flag that the app
should terminate? Is there a command I can use to terminate the entire
process? ExitThread won't do anything for me in the constructor. Should I
move the actual initialization out of the constructor? In Visual Basic I'd
believe I'd just have a "Public Sub New()" subroutine for the
initialization
and terminate with an "End" statement if I wanted to stop processing.

Thanks for any help!
Bill
 
Back
Top