e.KeyCode for abort program running

MiPakTeh 1,476 Reputation points
2021-09-06T09:56:33.723+00:00

Hi All,
What I try to do;

count number from 0 to 60 and set timer1.Interval=1000 after finish count then program start.
And if we want to abort that program start in between 0 to 60 (count) just press button F2.

somebody can show the true coding.
Thank.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Abort_
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Timer timer1;
        private int counter = 10;

        System.Diagnostics.Process process = new System.Diagnostics.Process();

        public Form1()
        {
            InitializeComponent();

            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000; // 1 second
            timer1.Start();
            label1.Text = counter.ToString();

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            counter--;

            if (counter == 0)
            {

                timer1.Stop();
                label1.Text = counter.ToString();

                process.StartInfo.UseShellExecute = true;
                process.StartInfo.FileName = "cmd";
                process.StartInfo.CreateNoWindow = true;
                process.Start();
            }

            else if(counter > 0)
            {
                this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Some);
            }
        }
        private void Some(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt)
            {
                System.Environment.Exit(0);
            }
        }

    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,523 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,411 Reputation points
    2021-09-06T12:23:37.823+00:00
    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.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,286 Reputation points
    2021-09-06T16:19:16.18+00:00

    Personally I recommend System.Threading.Timer. More code while maintaining more control and usable in non-windows forms projects.

    Get full source

    129675-f1.png

    using System;  
    using System.ComponentModel;  
    using st = System.Threading;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
      
    namespace CounterExample  
    {  
        public partial class MainForm : Form  
        {  
            st.Timer _counterTimer;  
              
            public int _numberOfSecondsTrigger = 60;  
            public int _timesExecutes = 1;  
            private int _seconds = 0;  
              
            public MainForm()  
            {  
                InitializeComponent();  
                  
                Shown += OnShown;  
                Closing += OnClosing;  
                  
            }  
      
            private void OnClosing(object sender, CancelEventArgs e)  
            {  
                _counterTimer.Dispose();  
            }  
      
            private void OnShown(object sender, EventArgs e)  
            {  
                StartTimer();  
            }  
      
            private void StartTimer()  
            {  
                _counterTimer = new st.Timer(Dispatcher);  
                _counterTimer.Change(1, st.Timeout.Infinite);  
            }  
      
            private async void Dispatcher(object state)  
            {  
                _counterTimer.Dispose();  
                await RunJob();  
                StartTimer();  
            }  
      
            private async Task RunJob()  
            {  
                _seconds++;  
      
                if ( _seconds > _numberOfSecondsTrigger )  
                {  
                    _seconds = 0;  
                      
                    TimesLabel.InvokeIfRequired(label => label.Text = _timesExecutes.ToString());  
                      
                    _timesExecutes += 1;  
                      
                    StartTimer();  
      
                }  
                 
                lblView.InvokeIfRequired( label => label.Text = TimeSpan.FromSeconds(_seconds).ToString("ss") );  
                  
                await Task.Delay(1000);  
            }  
             
              
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if (keyData != (Keys.Alt | Keys.F2)) return base.ProcessCmdKey(ref msg, keyData);  
                Environment.Exit(0);  
                return true;  
      
            }  
      
        }  
    }