Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!

C#
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");
        }
Posted
Updated 18-May-15 1:57am
v3
Comments
Andy Lanng 18-May-15 6:20am    
All that the input pipe does is send a signal to your app.
You are asking for a way to, on a signal (timer) have the console send your app a signal.

What's the point? just use the timer as the signal
Member 11543226 18-May-15 6:22am    
But in OnTimedEvent I have to press enter so that all Application does its task. I want Exactly the same way.
Andy Lanng 18-May-15 6:38am    
Just call the same method. You cannot write to the input stream from within the application.
Sinisa Hajnal 18-May-15 7:12am    
on timer, don't send Enter key (or any other), simply call the task that you would call on enter.
Member 11543226 18-May-15 6:46am    
I didnt get your point sir?

1 solution

Take a look:

C#
            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 ^_^
 
Share this answer
 
v2

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