using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;namespace Ch13Ex01
{
    class Program
    {
        static int counter = 0;
        static string displayString = "This string will appear one letter at a time.";
        static void Writechar(object source, ElapsedEventArgs e)
        {
            Console.Write(displayString[counter++ % displayString.Length]);
        }        static void Main(string[] args)
        {
            Timer myTimer = new Timer(100);
            myTimer.Elapsed += new ElapsedEventHandler(Writechar);
            myTimer.Start();
            Console.ReadKey();
        }
        
    }
}
请大侠帮忙解释一下这个程序的详细流程,从入口到结束,以及是怎么调用Writechar()的? 我在vs里面一步步的执行,程序并没有跳进Writechar()函数中,很迷惑啊··   新手第一次发帖,在这里先感谢各位了。

解决方案 »

  1.   

    把断点打到这个Writechar方法中,会进来的。
      

  2.   


    //找我的理解你似乎是想问这个程序是怎么开始执行的?如果不是的话请当我没说.过于没学好见笑...
           static void Main(string[] args)//程序入口点方法 从这里开始执行
            {
                Timer myTimer = new Timer(100);//实例化一个时间对象,并设置为每间隔1毫秒触发一次事件.
                myTimer.Elapsed += new ElapsedEventHandler(Writechar);//将Writechar方法作
                  //为参数绑定到时间控件(myTimer )的事件中.(每隔1毫秒触发一次)
                myTimer.Start();//设置时间控件的enabled的属性为true(启动时间控件)并开始触发Elapsed 事件.
                Console.ReadKey();
            }
      

  3.   


    恩,我试了试,果然进来了。 只是还有另外一个疑问,程序是从Console.ReadKey()跳到Writechar函数中的,可是我感觉应该是从myTimer.Start()跳过去呢?
      

  4.   


    先谢谢您的详细解释。 这个流程我知道,只是不知道从哪个地方跳到Writechar()函数中的,我觉得应该是执行完myTimer.Start()后跳转,可vs显示是执行完Console.ReadKey()后跳转的?
      

  5.   

    执行完myTimer.Start()后1毫秒(非绝对)跳转.
      

  6.   


                Timer myTimer = new Timer(100);//定义了一个定时器,时间间隔为100毫秒
                myTimer.Elapsed += new ElapsedEventHandler(Writechar);//注册事件,每隔100毫秒就会调用Writechar()方法
                myTimer.Start();//启动定时器,于是每100毫秒都会触发Elapsed事件,对应执行的方法是Writechar()
                Console.ReadKey();
      

  7.   

     可我在vs里面但不执行的时候,显示的是执行过console.ReadKey()后跳转的?
      

  8.   

    谢谢~ 可我在vs里面但不执行的时候,显示的是执行过console.ReadKey()后跳转的? 如果一直计时的话,console.ReadKey() 应该是执行不到的吧?
      

  9.   

    具体时间是执行到myTimer.Start()方法之后触发的,每100毫秒执行一次Writechar()方法,计算机的执行能力很快的,100ms执行到Console.ReadKey(),不是很正常么,所以你看起来会是执行完Console.ReadKey()后跳转
      

  10.   

    这个timer是相当于是另一个线程,不影响程序往下执行到Console.ReadKey()的,多线程一样的,明白了吗
      

  11.   


    也就是说,执行到myTimer.Start()后触发计时器,在100毫秒后执行Writechar()方法,而在尚未到100毫秒的时候,已经把Console.ReadKey()执行过了?  额,明白了~~ 十分感谢啊!!