【问题标题】:how to display a message for a certain amount of time in C#如何在 C# 中显示一定时间的消息
【发布时间】:2019-03-15 14:28:08
【问题描述】:

如何在 C# 中显示一定时间的消息?我想显示消息“Hello World”1 秒。这是我的程序。我不知道为什么它不起作用。我在等 3 秒。所以在这前 3 秒内它应该已经引发了事件 .elapsed 对吗?

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

【问题讨论】:

  • 为什么是 lorum ipsum 的盆栽历史?
  • 你在哪里打电话Start()?您期望的确切输出是什么?
  • 你没有启用定时器。
  • 显示“线程即将休眠”消息,然后休眠 3 秒的目的是什么?这远没有您的代码所描述的那么复杂。事实上,它只有 3 行:Console.WriteLine("Hello World"); Thread.Sleep(1000); Console.Clear(); 顺便说一句,Timer 最适合需要在每个指定间隔引发的事件。如果您只想做某事一次,请让线程休眠(或 Task.Delay() 用于 Task),然后执行该操作。

标签: c# console-application


【解决方案1】:

你需要启动你的计时器

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

        }
    }
}

【讨论】:

  • 感谢您的意见,我做了这些更改。但它仍然第二次打印 Hello World(它不止一次引发事件)
  • 把这个写在start()之前,那么它会发生一次
  • aTimer.AutoReset = false;
  • 把这个写在start()之前,然后它会发生一次:你是什么意思
  • 我编辑添加行的代码,这样,事件在每 3s 睡眠期间仅发生一次。
猜你喜欢
  • 2016-09-25
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多