如何在控制台应用程序中以编程方式按Enter键而不使用键盘 [英] How to programmatically press Enter button without keyboard in Console Application

查看:70
本文介绍了如何在控制台应用程序中以编程方式按Enter键而不使用键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写在没有键盘按下的情况下每5分钟后以编程方式按下输入按钮的功能。我想按Enter键后运行所有方法。我正在尝试跟随,但没有工作。



帮助我!



I am trying to write function which press enter button programmatically after every 5 minutes without keyboard press.I want to run all methods after pressing enter. I am trying following but doesn't worked.

help me!

namespace ConsoleApplication2
{
    class Program
    {
        string path = @"somepath";

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SaiNathHospital"].ToString());

        public void getConsoleInput()
        {
            try
            {
                FileInfo fi = new FileInfo(path);

                for (int i = 0; i <= 0; i++)
                {
                    Console.WriteLine("");
                    using (StreamWriter sw = new StreamWriter(path))
                    {
                        sw.WriteLine(Console.ReadLine());
                        sw.Close();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void getConsoleInputAtRuntime()
        {
 
        }

        public void ReadWriteIntoFile()
        {
            try
            {
                string filename = @"somepath";
                StringBuilder sb = new StringBuilder();

                StreamReader sr = new StreamReader(path);
                string s = sr.ReadLine();
                sr.Close();

                DataExport("Select * from PATIENT_TABLE where [BARCODE] = '" + s + "'", filename);
            }
            catch { }
        }

        public void DataExport(string SelectQuery, string filename)
        {
            try
            {
                using (var dt = new DataTable())
                {
                    using (var da = new SqlDataAdapter(SelectQuery, con))
                    {
                        da.Fill(dt);
                        var rows =
                            from dr in dt.Rows.Cast<DataRow>()
                            select String.Join(
                                ",",
                                from dc in dt.Columns.Cast<DataColumn>()
                                let t1 = Convert.IsDBNull(dr[dc]) ? "" : dr[dc].ToString()
                                let t2 = t1.Contains(",") ? String.Format("\"{0}\"", t1) : t1
                                select t2);

                        using (var sw = new StreamWriter(filename))
                        {
                            // sw.WriteLine(header);
                            foreach (var row in rows)
                            {
                                sw.WriteLine(row);
                            }
                            sw.Close();
                        }
                    }
                }
            }
            catch (Exception e) { Console.WriteLine(e.Message); }
        }

        public void WriteFileOutput()
        {
            string path = @"some path";
            if (File.Exists(path))
            {
                string[] lines = File.ReadAllLines(path);

                foreach (string line in lines)
                {
                    Console.WriteLine(line);
                }

            }
            Console.ReadLine();
        }

        public static void Main(string[] args)
        {
            Program p = new Program();
            p.timerfor();
            p.getConsoleInput();
         p.ReadWriteIntoFile();
            p.WriteFileOutput();
        }

 public void timerfor()
        {
            Timer t = new Timer(5000);
            t.Elapsed += OnTimedEvent;
          //  t.Interval = 5000;
            t.Start();
            t.Enabled = true;          
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
           // System.Windows.Forms.SendKeys.Send("{ENTER}");
            System.Windows.Forms.SendKeys.Send("~");
          //  System.Windows.Forms.SendKeys.SendWait("ENTER");
        }

推荐答案

看看:



Take a look:

            static Timer _timer = new Timer(5000);
            public static void Main2(string[] args)
            {
                Program p = new Program();

                _timer.Elapsed += OnTimedEvent;
                
                // This thread will run in the background for the lifetime of the app.
                new Thread(() =>
                {
                    while (true)
                    {
                        //Any time enter is pressed it will set the _resetEvent & stop the timer
                        Console.ReadLine();
                        _resetEvent.Set();
                        _timer.Stop();
                    }
                }).Start();


                p.getConsoleInput();
                p.ReadWriteIntoFile();
                p.WriteFileOutput();
            }
            
            //This is a Manual Reset Event.  Look it up for details
            private static ManualResetEvent _resetEvent = new ManualResetEvent(false);

            // The timer can also set the _resetevent and stop itself from executing a second time (if that's what you want)
            private static void OnTimedEvent(Object source, ElapsedEventArgs e)
            {
                _resetEvent.Set();
                _timer.Stop();
            }

//...

            public void WriteFileOutput()
            {
                string path = @"some path";
                if (File.Exists(path))
                {
                    string[] lines = File.ReadAllLines(path);

                    foreach (string line in lines)
                    {
                        Console.WriteLine(line);
                    }

                }

                //I guess this is where u want to wait?
                //Reset the _resetEvent in case it is currently set
                _resetEvent.Reset();
                //Start the timer
                _timer.Start();
                //Wait for reset event to be set.
                _resetEvent.WaitOne();

                // The code will not continue past the wait until either the enter button is clicked or the timer event runs.

            }





如果那不是您所需要的那么请让我知道



这里有一些先进的技术。如果您有任何问题,请将它们发布为新的,以便我们的评论中的朋友可以获得一些积分:)



祝你好运^ _ ^



If that is not what you need then please let me know

There are some advanced techniques in here. If you have any questions then post them as new ones so our friends in comments can pick up some points :)

Good luck ^_^


这篇关于如何在控制台应用程序中以编程方式按Enter键而不使用键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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