按任意键可防止控制台应用程序关闭 [英] Prevent console application from closing on pressing any key

查看:155
本文介绍了按任意键可防止控制台应用程序关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友

如果用户意外按任意键,我不想关闭控制台应用程序。该功能将每10秒调用/触发





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


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;
        }







谢谢

Bala




Thanks
Bala

推荐答案

尝试:

Try:
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");
        }


这篇关于按任意键可防止控制台应用程序关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆