Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends
I don't want close the console application if the user press any key accidentally. The function will be called/Triggered for every 10 sec


C#
static void Main(string[] args)
        {
            SetTimer = new System.Timers.Timer(10000);
            SetTimer.Elapsed += OnTimedEvent;
            SetTimer.Enabled = true;
            Console.ReadLine();

        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            SetTimer.Enabled = false;
            MYMethod();
            SetTimer.Enabled = true;
        }



Thanks
Bala
Posted
Comments
Sergey Alexandrovich Kryukov 22-Aug-15 1:30am    
The question makes no sense at all. An application won't be closed "on pressing any key"; there is nothing to prevent.
And your idea to use the timer is such a big abuse...
To get help, you need to explain what you really need to achieve. Forget about, keys, timers, and other technical detail, just explain the purpose.
—SA

1 solution

Try:
C#
static void Main(string[] args)
        {
            SetTimer = new System.Timers.Timer(10000);
            SetTimer.Elapsed += OnTimedEvent;
            SetTimer.Enabled = true;
            string inp;
            do
            {
                inp = Console.ReadLine();
            } while (inp != "Q");
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900