zoukankan      html  css  js  c++  java
  • WPF的System.Windows.Threading.DispatcherTimer的使用(每隔一定的时间重复做某事)

    这里使用了一个进度条来展示,

    前段代码:

     1 <Window x:Class="TimerTest.MainWindow"
     2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     4         Title="MainWindow" Height="350" Width="525">
     5     <Grid>
     6         <Button Content="Button" HorizontalAlignment="Left" Margin="241,249,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
     7         <ProgressBar x:Name="pb" Minimum="0" Maximum="100" HorizontalAlignment="Left" Height="93" Margin="10,151,0,0" VerticalAlignment="Top" Width="497"/>
     8 
     9     </Grid>
    10 </Window>
    View Code

    后台代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Windows;
     7 using System.Windows.Controls;
     8 using System.Windows.Data;
     9 using System.Windows.Documents;
    10 using System.Windows.Input;
    11 using System.Windows.Media;
    12 using System.Windows.Media.Imaging;
    13 using System.Windows.Navigation;
    14 using System.Windows.Shapes;
    15 using System.Windows.Threading;
    16 
    17 namespace TimerTest
    18 {
    19     /// <summary>
    20     /// Interaction logic for MainWindow.xaml
    21     /// </summary>
    22     public partial class MainWindow : Window
    23     {
    24         public MainWindow()
    25         {
    26             InitializeComponent();
    27         }
    28 
    29         private void Button_Click(object sender, RoutedEventArgs e)
    30         {
    31             DispatcherTimer dispatcherTimer = new DispatcherTimer();
    32             dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    33             dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    34             dispatcherTimer.Start();
    35         }
    36 
    37         private void dispatcherTimer_Tick(object sender, EventArgs e)
    38         {
    39             pb.Value += 3;
    40         }
    41     }
    42 }
    View Code
  • 相关阅读:
    项目中看似很难的问题可能很简单
    ASP.NET数据列表“全选”,批量处理的JS实现
    [转]给网站增加如:flv,torrent等特殊后缀格式文件下载
    GD Graphics Library
    在VS2008中编译64位程序以及遇到的问题
    关于C#闭包
    SNMP协议介绍及SNMP library
    ASP.NET MVC 使用总结(二)——扩展HtmlHelper实现动态生成title及meta
    LINQ查询代码整理(一)
    使用方便的SDK帮助文档
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/4987750.html
Copyright © 2011-2022 走看看