This is the demonstration of how to use a dispatch timer in WPF using C#.
private DispatcherTimer timer; // Declaring instance of DispatcherTimer class
public Window1()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
timer = new DispatcherTimer(); // Initializing the timer
timer.Interval = TimeSpan.FromSeconds(1); //Adding Timer Interval
timer.Tick += timer1_Tick; //Adding Tick Event for Timer.Tick function
timer.Start(); //Starts Timer
}
private void timer1_Tick(object sender, EventArgs e)
{
timer.Stop(); //Stops Timer
this.Close(); //Closes current form
}
0 Comment(s)