i need to exit out of a console program i'm making nicely. meaning, i need to do some cleanup before actually quitting the program. for instance, i would need to close a mysql connection.
here's the code i have so far that i've found from other sites:
this code will actually work, but only if you use CTRL+C instead of the X in the corner of the console. the program will wait until the OnControlEvent processes. however, if you use the X in the corner, the program will Breakpoint there, but it will still exit itself, breaking past the breakpoint. i think that this is a drawback of the OS killing the thread, and not a real problem with the console program.
has anybody had to do a similar kind of thing for a console program, and would you be willing to post your code to try out?
also, it's possible for me to use this code now, just limit the closing of it to CTRL+C, but how do i get to my connect.Close(); from inside the OnControlEvent? connect is referenced at the top of the code snippet, which is outside of the main.
here's the code i have so far that i've found from other sites:
// test mysql area
MySqlCommand cmd = new MySqlCommand();
Object returnValue;
MySqlConnection connect = new MySqlConnection("<removed>");
// end area
/// <summary>
/// Starts up the program
/// </summary>
/// <param name="args">The args.</param>
static void Main(string[] args)
{
SetConsoleCtrlHandler(new ControlEventHandler(OnControlEvent), true);
try
{
Process[] pr;
pr = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (pr.Length > 1)
{
//Console.WriteLine("Only 1 instance of SingleInstanceTest is allowed...");
Environment.Exit(0);
}
else
{
//Console.WriteLine("There can be only 1...");
}
//Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Program p = new Program();
p.Config();
p.Setup();
p.Start();
p.Goooo();
}
public enum ConsoleEvent
{
CTRL_C = 0,
CTRL_BREAK = 1,
CTRL_CLOSE = 2,
CTRL_LOGOFF = 5,
CTRL_SHUTDOWN = 6
}
public delegate void ControlEventHandler(ConsoleEvent consoleEvent);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool SetConsoleCtrlHandler(ControlEventHandler e, bool add);
public static void OnControlEvent(ConsoleEvent consoleEvent)
{
//connect.Close();
Console.WriteLine("Event: {0}", consoleEvent);
Console.ReadLine();
}
this code will actually work, but only if you use CTRL+C instead of the X in the corner of the console. the program will wait until the OnControlEvent processes. however, if you use the X in the corner, the program will Breakpoint there, but it will still exit itself, breaking past the breakpoint. i think that this is a drawback of the OS killing the thread, and not a real problem with the console program.
has anybody had to do a similar kind of thing for a console program, and would you be willing to post your code to try out?
also, it's possible for me to use this code now, just limit the closing of it to CTRL+C, but how do i get to my connect.Close(); from inside the OnControlEvent? connect is referenced at the top of the code snippet, which is outside of the main.