Context: the application I’m trying to make does not display a form initially, but only an open file dialog. After selecting a file the application may exit or open a form.
I’m having trouble closing my application after calling Application.Run(). The following example does not produce an application that kills itself.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var context = new Context();
Application.Run(context);
Console.Beep();
}
}
class Context : ApplicationContext
{
public Context()
{
Application.Exit();
}
}
On a side note, what preparations do I need to make before I can open a form? Do I need to have a call to Application.Run before I can show forms?
Thanks a bunch! XOXO
You could try moving the exit logic to a new method:
and have
Application.Runcallcontext.Exit()when neededThe problem with the original approach is that you placed the exit logic in the constructor of
Context, which means every time you create aContextobject, you have the potential to exit your program (it is a certainty in the case of the code you posted). Instead haveContextcapture all relevant (context) information and make the decision to exit based on that info when an explicit method call is made after it was created.