using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace GaugeSamples
{
    public partial class Clock : UserControl
    {
        public Clock()
        {
            InitializeComponent();

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                Thread thread = new Thread(new ThreadStart(RunClock));
                thread.Start();
            }
        }

        private delegate void UpdateUIDelegate();
        private Timer _timer;

        private void RunClock()
        {
            _timer = new Timer(
                new TimerCallback((target) => 
                { 
                    var action = new UpdateUIDelegate(UpdateClock);
                    Dispatcher.BeginInvoke(action);
                }), 
                null, 
                TimeSpan.FromSeconds(0), 
                TimeSpan.FromSeconds(1))
                ;
        }

        private void UpdateClock()
        {
            var now = DateTime.Now;
            clockHours.Value = now.Hour % 12 + now.Minute / 60.0;
            clockMins.Value = now.Minute;
            clockSecs.Value = now.Second;
        }
    }
}
