如何在C#中显示消息一定时间

时间:2019-03-15 14:28:08

标签: c# console-application

如何在C#中显示消息一定时间?我想显示消息“ Hello World” 1秒钟。这是我的程序。我不知道为什么它不起作用。我正在等待3秒钟。因此,在最初的3秒钟内,它应该引发该事件。过去了吗?

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

1 个答案:

答案 0 :(得分:0)

您需要启动计时器

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!");

        }
    }
}