Terminating an Application ?
-
Talking about the designers of C# making something complicated or what! I have built a LogViewer application and my objective is to terminate the application if it determines there are no log files to display or if the logfile is in use by another application. I run the application from Main as follows: Application.Run(new MainForm()); In MainForm, it first does InitializeComponent, then it Populates the LogSelector with some user choices and then it adds logging info to the appropriate control if a log file is available (DisplayLogInfo). If DisplayLogInfo detects that no logfile is available or the logfile is being used by another app, I want LogViewer to not show any form and to terminate. I have tried this.Hide(). I have tried Application.Exit(). I have tried to make the form invisible by setting this.Visible to false. I have tried this.Dispose(), but then I get an unhandled exception and would rather not handle the exception by ignoring it. Regardless, the form always displays and I have to close the app via closing the Window by the Close button. Can anyone suggest a way to get this to work. I would rather not rewrite the app. so that it checks for the logfile before running InitializeComponent as I believe there must be a way to accomplish the task as I have written it and I would like to learn how it can be done. Comments?
-
Talking about the designers of C# making something complicated or what! I have built a LogViewer application and my objective is to terminate the application if it determines there are no log files to display or if the logfile is in use by another application. I run the application from Main as follows: Application.Run(new MainForm()); In MainForm, it first does InitializeComponent, then it Populates the LogSelector with some user choices and then it adds logging info to the appropriate control if a log file is available (DisplayLogInfo). If DisplayLogInfo detects that no logfile is available or the logfile is being used by another app, I want LogViewer to not show any form and to terminate. I have tried this.Hide(). I have tried Application.Exit(). I have tried to make the form invisible by setting this.Visible to false. I have tried this.Dispose(), but then I get an unhandled exception and would rather not handle the exception by ignoring it. Regardless, the form always displays and I have to close the app via closing the Window by the Close button. Can anyone suggest a way to get this to work. I would rather not rewrite the app. so that it checks for the logfile before running InitializeComponent as I believe there must be a way to accomplish the task as I have written it and I would like to learn how it can be done. Comments?
Add a handler for MainForm's Load event, and execute this.Close() inside that handler. E.g.:
private void MainForm_Load(object sender, System.EventArgs e)
{
// Insert code to determine whether logfile is
// available.if (LogFileIsNotAvailable) { this.Close(); }
}
-
Add a handler for MainForm's Load event, and execute this.Close() inside that handler. E.g.:
private void MainForm_Load(object sender, System.EventArgs e)
{
// Insert code to determine whether logfile is
// available.if (LogFileIsNotAvailable) { this.Close(); }
}
I see that your suggestion should work; however, I am still a little puzzled. Can you tell me the reason why the "this.Hide()" or "this.Close()" did not work when implemented within the MainForm constructor?
-
I see that your suggestion should work; however, I am still a little puzzled. Can you tell me the reason why the "this.Hide()" or "this.Close()" did not work when implemented within the MainForm constructor?
First, I gave you bad (or at least incomplete) advice. I meant to say to use the Application.Idle event rather than the Load event. Calling Close from the Load event can cause a memory leak. You could do something like:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();// Code to determine whether to close form if (ShouldClose) { Application.Idle += new EventHandler(Application\_Idle); } } void Application\_Idle(object sender, EventArgs e) { this.close(); }
}
The Hide and Close methods don't work in the constructor because the Form is still being constructed. And in any case you don't want to close the form at this point, assuming the calling code looks something like:
Application.Run(new MainForm());
If MainForm is closed and disposed before it is constructed, then Application.Run will throw an ObjectDisposedException.