0

Good morning,

I have several questions but I am not sure which is the important one to ask so I'll first state my overall problem. I can't close my Winform App. I have searched and found many answers but they either don't work, I don't understand or both.

If I do all my work and then call Application.Exit the form never closes. Same results if I put this.Close. However if I place a button on the form and call Application.Exit it closes the form.

I obviously do not understand the flow, I hope it is clear to someone what I am trying to do. As a non-programmer I have been piecing this project together for a few months and this is my last step - Close the form after work is complete if it was run from command line with arguments. I would try longer to work it out but my Visual Studio trial runs out this week so I turn to the experts :)

Thank you, Todd

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProgramCSToormTest
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(String[] args)
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //add if
        if (args.Length == 0)
        {
            Application.Run(new Form1("Form"));
        }
        else
        {
            Application.Run(new Form1(args[0]));

        }
    }


}
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProgramCSToormTest
{

public partial class Form1 : Form
{
    string CLArg1;
    string ReturnText;
    public Form1(string Arg1)
    {
        InitializeComponent();
        if (Arg1 != null)
        {
            CLArg1 = Arg1;
            textBox1.Text = Display(CLArg1);
            //button1.Enabled = false;
        }
        else
        {
            textBox1.Text = "click button to start";
        }
        Application.Exit(); //This seems to be ignored
    }

    public void button1_Click(object sender, EventArgs e)
    {
        CLArg1 = null;
        textBox1.Text = Display("Hello World");
        Application.Exit();
    }
    public string Display(string DisplayText)
    {
        if (CLArg1 != null)
        {
            ReturnText = CLArg1;
            return(ReturnText);

        }
        else
        {
            ReturnText = DisplayText;
            return(ReturnText);
        }

    }
}
}    
  • 1
    You can download visual studio express for c#. Its free, and will allow you to continue. :) – crthompson Oct 07 '14 at 15:43
  • 2
    You're trying to exit the application in the constructor would be my bet as to why it doesn't work. What's the point in using a form if you're going to get rid of the form before it's created. – tbddeveloper Oct 07 '14 at 15:45
  • Thanks @paqogomez - I'll have to check that out. – Todd Saettele Oct 07 '14 at 15:52
  • http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx – crthompson Oct 07 '14 at 15:53
  • @Hammerstein - Thank you - This is the flow I guess I don't understand. I will have to look into constructors. I really don't want the form to show at all if it is run with arguments but if it is launched without I need the form. I couldn't figure out how to keep it from launching while still accessing the logic in form1.cs so now I just want to close it. – Todd Saettele Oct 07 '14 at 15:58
  • http://stackoverflow.com/questions/11561303/run-c-sharp-application-as-scheduled-task you could do something like this, then bundle up the behavior of your application in a class that both the form and the application can call. – tbddeveloper Oct 07 '14 at 16:01
  • Thank you @Hammerstein - The link you posted is the situation I find myself in. I have a form that needs to run as a scheduled task as well. From reading through this it sound like a shared library is what I want. Code that can be accessed from Program.cs if run from a command line with Args or the form if no Args or launched from EXE. Does that sound right? If so I am off to learn about shared libraries. Thank you for your time! – Todd Saettele Oct 07 '14 at 18:27
  • It is the route I would go down. – tbddeveloper Oct 07 '14 at 18:36
  • This is turning into a project so I just want to make sure I am on the right path. The best I can figure is that a shared library is creating a DLL so I crated a new Class Library Project and I am moving all the functions from my Windows form.cs to the New Class Library. I just want to verify because it looks like it will be quite a bit of work for me to move the functions since many of them are "form aware" so certain functions reference textboxes, buttons and radioButtons within the function. – Todd Saettele Oct 07 '14 at 21:51

2 Answers2

1

See this question. Application.Close() only works if an application has been created. This is done by calling Application.Run(). Now. in your code you call Application.Exit() from the constructor of your form. Which is executed before the Application.Run() that is needed to create the application.

To fix this, either wait until after Application.Run(). Or, if you want to quit the application in the constructor use Environment.Exit(int statusCode). When using Environment.Exit(int statusCode) keep this in mind though.

Community
  • 1
  • 1
TheDutchDevil
  • 826
  • 11
  • 24
  • 1
    It should be noted that `Application.Exit, Close, etc.` should never be put into a constructor. – TyCobb Oct 07 '14 at 15:51
  • Indeed. Constructors are meant to set up an object, and they should not be used to do any haevy lifting. In case you really want to exit the application from a constructor make sure to mention it in the comments above the constructor. But it should be avoided as much as possible. – TheDutchDevil Oct 07 '14 at 15:56
  • Thanks @TheDutchDevil and TyCobb - I will have to check out the links to see if I can make sense of what you are telling me. I guess my mistaken belief is that the Application.Run in Program.cs happens before what I think you are calling the constructor. – Todd Saettele Oct 07 '14 at 16:05
  • @ToddSaettele `public Form1(string Arg1)` is the constructor for form1. Note the class name is the method name without a return identifier (like void) – crthompson Oct 07 '14 at 16:51
  • Thanks @paqogomez - I am seriously lacking in terminology so this helps a lot, simple explanations! – Todd Saettele Oct 07 '14 at 18:28
0

You can't close the Application when the Form is Loading from the Program class. Try calling the Exit method after Loading the Form:

 private void Form1_Load(object sender, EventArgs e)
    {
        if (CLArg1 != String.Empty)

        Application.Exit(); 
    }
MikeG
  • 523
  • 4
  • 13