using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace Abort_ {
public partial class Form1 : Form {
private Timer timer1;
private int counter = 10;
private Process process;
public Form1() {
InitializeComponent();
timer1 = new Timer();
timer1.Tick += timer1_Tick;
timer1.Interval = 1000; // 1 second
timer1.Start();
label1.Text = counter.ToString();
KeyUp += Form1_OnKeyUp;
}
private void timer1_Tick(object sender, EventArgs e) {
counter--;
label1.Text = counter.ToString();
if (counter == 0) {
timer1.Stop();
KeyUp -= Form1_OnKeyUp;
process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "cmd";
process.StartInfo.CreateNoWindow = true;
process.Start();
}
}
private void Form1_OnKeyUp(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.F2 && e.Alt) {
Environment.Exit(0);
}
}
}
}
This is pretty much what you had except I've used a KeyUp event instead of KeyDown, as not all events were firing for KeyDown on Form1. Adding the KeyUp handler has been moved to the constructor, and the unbinding is done inside the tick (when counter reaches zero.)
Updating label1.Text has been moved outside of your if statement so it updates every tick.