2
Answers

a question about System.Threading.Timer

usfish

usfish

17y
2.2k
1

Why the following program only output a single line of  "World"? I expect it to output "Hello" every second and finally "World"

Also, how to control the how long the timer will work? Or, in this program, how to control the number of "Hello"s  it outputs?

Thanks

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication5
{
    class Program
    {
        public int count = 0;
        static void Main(string[] args)
        {
            //TimerCallback command = new TimerCallback(SendCommand);
            Timer t = new Timer(SendCommand, null, 0, 1000);
            Console.WriteLine("World");
        }

        public static void SendCommand(object s)
        {
            Console.WriteLine("Hello");
        }
    }
}

 

Answers (2)