Search code examples
xamarin.iosstopwatch

C#/MonoTouch implement a StopWatch showing time on a label dynamically


Is it possible to implement a stopwatch showing stopwatch values changing on label as watch is running.

I have added stopwatch using following code

Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();

// Stop timing
stopwatch.Stop();

How should i show time running on a label ????


Solution

  • Use a Timer instead of a Stopwatch.

    // create a timer that fires every 10 ms
    Timer aTimer = new Timer(10);
    
    // Hook up the Elapsed event for the timer.
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    
    aTimer.Enabled = true;
    
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        // update your label here
    }