Mega Code Archive

 
Categories / C# Tutorial / WPF
 

Using DispatcherTimer to trigger event

using System; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading;     public class MainClass : Window     {         RadialGradientBrush brush;         double angle;         [STAThread]         public static void Main()         {             Application app = new Application();             app.Run(new MainClass());         }         public MainClass()         {             WindowStartupLocation = WindowStartupLocation.CenterScreen;             Width = 400;                   Height = 400;             brush = new RadialGradientBrush(Colors.White, Colors.Blue);             brush.Center = brush.GradientOrigin = new Point(0.5, 0.5);             brush.RadiusX = brush.RadiusY = 0.10;             brush.SpreadMethod = GradientSpreadMethod.Repeat;             Background = brush;             DispatcherTimer tmr = new DispatcherTimer();             tmr.Interval = TimeSpan.FromMilliseconds(100);             tmr.Tick += TimerOnTick;             tmr.Start();         }         void TimerOnTick(object sender, EventArgs args)         {             Point pt = new Point(0.5 + 0.05 * Math.Cos(angle),0.5 + 0.05 * Math.Sin(angle));             brush.GradientOrigin = pt;             angle += Math.PI / 6;      // ie, 30 degrees         }     }