Timer.Stop() can' work?

chuawenching

Regular
Joined
Jul 26, 2003
Messages
66
Hi there.

I am displaying a string which will appear 1 char at a time. The problem is after it display the 1st string, it will keep on continue.

When i uncomment the myTimer.stop(), it does not even run at all.. cannot see anything on the command prompt.

Any help?

using System;
using System.Timers;

namespace Handling_Events___1_char_at_a_time
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static int counter = 0;
static string displayString = "Hello World!";

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Timer myTimer = new Timer(100);

myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
myTimer.Start();

//myTimer.Stop();

Console.ReadLine();
}

static void WriteChar(object source, ElapsedEventArgs e)
{
Console.Write(displayString[counter++ % displayString.Length]);

}
}
}

Any help?

I am starting to learn events as well.

Regards,
Chua Wen Ching :p
 
This is your problem:
myTimer.Start();
myTimer.Stop();

The timer starts then stops right away. Put the timer in the
WriteChar method and stop it when the text is complete.
 
Back
Top