Timer events in WPF

Source: Internet
Author: User

The Timer control similar to WinForm is no longer available in WPF. Therefore, you need to use the DispatcherTimer class to implement scheduled execution events similar to Timer. This event is implemented using delegation. DispatcherTimer classInIn System. Windows. Threading, the using System. Windows. Threading namespace is required.

The following is a simple Sample Code. The Code displays the current system time in real time in the title of the WPF form.

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Data;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Imaging;
Using System. Windows. Navigation;
Using System. Windows. Shapes;
Using System. Windows. Threading;

Namespace TimerWindow
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
DispatcherTimer timer = new DispatcherTimer ();

Public MainWindow ()
{
InitializeComponent ();

Timer. Tick + = new EventHandler (timer_Tick );
// Timer. Interval = TimeSpan. FromSeconds (0.1); // set the Refresh Interval
Timer. Start ();
}

Void timer_Tick (object sender, EventArgs e)
{
This. Title = string. Concat ("TimerWindow", DateTime. Now. ToString ("yyyy-MM-dd hh: mm: ss "));
}

}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.