0

I'm still learning all the cool tricks C# has to offer. I have a messagebox alert that pops up after a timer expires. I'm wondering if it is at all possible to have the console application terminate on its own after the user click the "OK" button on the messagebox. The console app is automatically minimized to the taskbar if that of any circumstance.

Thanks in advance.

Daniel Vincent
  • 31
  • 2
  • 10

3 Answers3

5

Give this a go:

// Terminates this process and gives the underlying operating system the specified exit code.
Environment.Exit()

MSDN: Environment.Exit Method

Richard
  • 8,110
  • 3
  • 36
  • 59
  • Thank you. I had a feeling it was something very simple. I rarely work with console apps. I appreciate the quick answer. Works flawlessly. – Daniel Vincent May 16 '12 at 15:54
4

Use Environment.Exit()

http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx

dtsg
  • 4,408
  • 4
  • 30
  • 40
0

This is what I would have done.

Wrap your messagebox code into an if statement

    if (MessageBox.Show("error", "error", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
    {
        System.Diagnostics.Process.GetProcessesByName("YOURPROCESSNAME.EXE")[0].Kill();
    }

There is no errorhandling in here, and I take it your console runs as a process. If you use the name of the process your console is running in GetprocessesbyName, you can close it with this method.

To blatantly kill your app (stop process) you can also use Environment.Exit(0) instead of Process.Kill().

Eon
  • 3,833
  • 10
  • 46
  • 75
  • I made a little pointless, the if statement would be more effective if your messagebox has more buttons :D – Eon May 16 '12 at 15:53