TheRacc
TheRacc

Reputation: 13

Program opens cmd then closes

Title says it all, it opens a command prompt then closes in 1/2 a second. i dont know why and i havent found anything on this. i just made a new c# app. Might have something to do with Main as it has no reference.

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;
using System.Threading;

namespace dumb_thing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static void Main(string[] args)
        {

        }
        private void Animatetext_Click(object sender, EventArgs e)
        {
            Animatetext.Text = "";
            Thread.Sleep(700);
            Animatetext.Text = "P";
            Thread.Sleep(350);
            Animatetext.Text = "Pa";
            Thread.Sleep(350);
            Animatetext.Text = "Par";
            Thread.Sleep(350);
            Animatetext.Text = "Para";
            Thread.Sleep(350);
            Animatetext.Text = "Parad";
            Thread.Sleep(350);
            Animatetext.Text = "Parado";
            Thread.Sleep(350);
            Animatetext.Text = "Paradox";
            Thread.Sleep(350);
        }
    }
}


Upvotes: 1

Views: 148

Answers (1)

Mr Hery
Mr Hery

Reputation: 870

It seem your codes is about to run a form application. No console application.

But if you are trying to develop a console application, the console will close automatically because there is no command to make it stay or wait the next key press. So you can add a line to make the application stay before closing the Main() method.

static void Main(string[] args)
{
   //
   // your coding here
   //

   Console.ReadLine();
}

It is not a good practice to develop a console application via form application template. Unless to get debug output.

Editted

If you want to develop a Form app, you have to remove the Main method in your form class. The Main method is only called once to start the form but you no need to add in your Form classes.

Full Example for Form

Program.cs (Here is where the Main method is placed and called only once to start the other form.)

namespace WindowsFormsApp1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

And now, here is the other form classes.

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Make sure to includes all library needed. This example taken from VS2019 .NET Framework Form Application template.

Other Occurs

In VS, you can set to start your project with console or not. You can set to in your Project Properties > Application Tab > Output type set to Windows Application. By default this setting will set to template setting (if form: windows application and if console: console application).

For best practice in form application, use the debugger console and set output using Debug.WriteLine().

Upvotes: 2

Related Questions