0

how to display a message for a certain amount of time in C# ? I would like to display the message "Hello World" for 1 sec. here is my program. I can't figure out why it won't work. I am waiting 3 seconds. so within these first 3 sec it should have raised the event .elapsed right ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace Timer
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0; 

            while (Console.Read() != 'q')
            {
                System.Timers.Timer aTimer = new System.Timers.Timer();
                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                aTimer.Interval = 1000;

                Console.WriteLine("Thread about to sleep");
                Thread.Sleep(3000);
                Console.WriteLine("Thread woke up");

                if (counter > 0)
                {
                    aTimer.Elapsed -= new ElapsedEventHandler(OnTimedEvent);
                }

                ++counter; 
            }


            //System.Timers.Timer aTimer = new System.Timers.Timer();
            //aTimer.Start();

            //Thread.Sleep(1000);

            //System.Timers.Timer aTimer2 = new System.Timers.Timer();
            //aTimer2.Start();

            //aTimer.

            //diff = aTimer2 - aTimer; 

            //if()


            //DateTime time1 = DateTime.Now;
            //Thread.Sleep(1000);
            ////DateTime time2 = DateTime.Now;

            //System.TimeSpan diff = time2 - time1;

            //if (diff < (System.TimeSpan)3000)
            //{
            //    Console.WriteLine("Hello"); 

            //}

            //Console.WriteLine("Press \'q\' to quit the sample.");
            // while (Console.Read() != 'q') ;
        }

        // Specify what you want to happen when the Elapsed event is raised.
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
demonslayer1
  • 741
  • 2
  • 16
  • 24
  • 2
    Why the poted history of lorum ipsum? – Liam Mar 15 '19 at 14:31
  • Possible duplicate of [C# clear Console last Item and replace new? Console Animation](https://stackoverflow.com/questions/5027301/c-sharp-clear-console-last-item-and-replace-new-console-animation) – Liam Mar 15 '19 at 14:31
  • 1
    Where are you calling `Start()`? And what exact output are you expecting? – mm8 Mar 15 '19 at 14:34
  • You didn't enable the timer. – Michel Hanna Mar 15 '19 at 14:41
  • What is the purpose of displaying the "Thread about to sleep" message, then sleeping for 3 sec? This is nowhere near as complex as your code makes it out to be. In fact, it's barely 3 lines: `Console.WriteLine("Hello World"); Thread.Sleep(1000); Console.Clear();` As an aside, `Timer` is best for events you need to raise every specified interval. If you just want to do something once, sleep the thread (or `Task.Delay()` for `Task`), then do that thing. – Patrick Tucci Mar 15 '19 at 20:59

1 Answers1

0

You need to start your timer

using System;
using System.Threading;
using System.Timers;

namespace Timer
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;

            while (Console.Read() != 'q')
            {
                System.Timers.Timer aTimer = new System.Timers.Timer();
                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                aTimer.Interval = 1000;
                aTimer.AutoReset = false
                aTimer.Start();

                Console.WriteLine("Thread about to sleep");
                Thread.Sleep(3000);
                Console.WriteLine("Thread woke up");


                if (counter > 0)
                {
                    aTimer.Elapsed -= new ElapsedEventHandler(OnTimedEvent);
                }

                ++counter;
            }


            //System.Timers.Timer aTimer = new System.Timers.Timer();
            //aTimer.Start();

            //Thread.Sleep(1000);

            //System.Timers.Timer aTimer2 = new System.Timers.Timer();
            //aTimer2.Start();

            //aTimer.

            //diff = aTimer2 - aTimer; 

            //if()


            //DateTime time1 = DateTime.Now;
            //Thread.Sleep(1000);
            ////DateTime time2 = DateTime.Now;

            //System.TimeSpan diff = time2 - time1;

            //if (diff < (System.TimeSpan)3000)
            //{
            //    Console.WriteLine("Hello"); 

            //}

            //Console.WriteLine("Press \'q\' to quit the sample.");
            // while (Console.Read() != 'q') ;
        }

        // Specify what you want to happen when the Elapsed event is raised.
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            Console.WriteLine("Hello World!");

        }
    }
}
Chris M
  • 1
  • 2