Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET to C# question - End statement equivalent?

Status
Not open for further replies.

traceytr

Programmer
Mar 13, 2001
94
US
(I'm using VS 2003 with Framework 1.1.) I'll bet this is about as simple a question as it gets, but for the life of me I cannot find the equivalent C# statement to the simple VB.NET statment:

Code:
End

All I want to do is end the application. If, say for instance, validation fails, I want the application to write an event to the event log and END THE APPLICATION - stop execution right there. I have only a few reference books for C# and the Visual Studio help files. I cannot find the answer to this simple question. I found a couple of possibilities:

1) Application.Exit();
2) this.Dispose();

For some reason, neither one of these works. I'm probably not using them correctly.

Thanks in advance for any help.

Tracey

 
I'm not primarily a C# programmer, but this seems to work ...

It consists of two forms each with a button and a checkbox.

The button on form1 opens form2 and the button on form2 closes form2. On both forms, checking the checkbox closes the application:

Form1:
Code:
		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if(checkBox1.Checked)
			{
				MessageBox.Show("The Application is about to end!");
				Application.Exit();
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Opening Form2");
			Form f = new Form2();
			f.Show();
		}

Form2:
Code:
		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox1.Checked)
			{
				MessageBox.Show("The Application is about to end!");
				Application.Exit();
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Returning to Form1");
			this.Close();
		}

 
You may have something still on-going in application that doesn't allow it to close, so Application.Exit() doesn't work properly. If you are using it on a FormClosing method, you have to set the Property Cancel of CancelEventArgs e to true. It might also work from other place in the application if you declare a CancelEventArgs object and use it for Application.Exit()
 
I assume are aware of the effects of forcing an application to terminate, you can call the Process.Kill() method from the current process (see the static Process.GetCurrentProcess())
 
I'm brand new to C#, and I've learned only enough VB.NET to write a handful of small interfaces, and that's the extent of my .NET experience so far. Now we're changing course to C#. I'm very glad to get the opportunity to learn it, but my roots in .NET are maybe an inch deep if that. (I've been reading many helpful threads in this forum. I read one that recommended the book C# and the .NET Platform by Andrew Troelsen, which I'll buy and study like crazy. I appreciate this site so much.)

This is a console application, and so far I've only put in a few simple processes.

Even when I use Application.Exit(); at the beginning before any other processes execute, the code will not run in Debug mode but gives me a build error. Here is the error description:

R:\CSharpPractice\MyFirstConsoleApp\MyAppMain.cs(72): The type or namespace name 'Application' could not be found (are you missing a using directive or an assembly reference?)

I'm probably not referencing the correct class to use this. Where might I find out which class this belongs to?

Thanks again.

Tracey
 
You just need to exit the Main() method to end the console app by calling return.

Code:
static int Main()
{
    while(true)
    {
        ConsoleKeyInfo cki = Console.ReadKey();
        // exit app when Escape
        if (cki.Key == ConsoleKey.Escape) [b][COLOR=blue]return 0[/color][/b];
    }
}

The Application.Exit() works for winforms. You got the error because the compiler cannot find the Application class, found only in the System.Windows.Forms assembly. (which is not referenced by default when you create a Console app).

[wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top