I don't want to close my C# application when user press Ctrl+C, so I added the following code:
static void Main(string[] args)
{
// some checks (only one instance running etc.)
Start(args);
Console.ReadLine();
}
public static void Start(string[] args)
{
Console.CancelKeyPress += new ConsoleCancelEventHandler(UserClose);
// Start infinity timer
_timer.Interval = 1000;
_timer.Elapsed += NewRun;
_timer.AutoReset = true;
_timer.Start();
}
public static void NewRun(Object sender, System.Timers.ElapsedEventArgs e)
{
_timer.Stop();
// Do the run
_timer.Start();
}
public static void UserClose(object sender, ConsoleCancelEventArgs args)
{
Console.WriteLine("\nThe read operation has been interrupted.");
Console.WriteLine(" Key pressed: {0}", args.SpecialKey);
Console.WriteLine(" Cancel property: {0}", args.Cancel);
// Set the Cancel property to true to prevent the process from terminating.
Console.WriteLine("Setting the Cancel property to true...");
args.Cancel = true;
// Announce the new value of the Cancel property.
Console.WriteLine(" Cancel property: {0}", args.Cancel);
Console.WriteLine("The read operation will resume...\n");
}
But somehow the application always terminate after the UserClose function. How can I debug what my process terminate? And whats wrong with the code above?
UPDATE: Seems that the Main return (like René Vogt mentioned in the comments). But why is the time stopping?
Source: MSDN