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>

后台代码:

 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 }

时间: 2024-08-27 22:32:34

WPF的System.Windows.Threading.DispatcherTimer的使用(每隔一定的时间重复做某事)的相关文章

silverlight 定时器 System.Windows.Threading.DispatcherTimer

1 声明 2 System.Windows.Threading.DispatcherTimer _MessageControler; 3 4 //刷新 5 _MessageControler = new System.Windows.Threading.DispatcherTimer(); 6 _MessageControler.Interval = new TimeSpan(0, 0, 600); 7 _MessageControler.Tick += new EventHandler(_Me

线程:主线程、子线程 同步线程、异步线程 单线程、多线程 System.Threading与System.Windows.Threading

入门-------------------------------------------------------------------------------- 概述与概念    一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为“主线程”)自动创建. 创建和开始使用多线程    public Window1()    {        //主线程         //Code……        //使用匿名方法来启动子线程        Thread t = new Th

关于WPF 中 “System.Windows.Markup.XamlParseException”类型的未经处理的异常在 PresentationFramework.dll 中发生 异常的处理。

前几天做一个项目的时候出现了一个异常: “System.Windows.Markup.XamlParseException”类型的未经处理的异常在 PresentationFramework.dll 中发生 其他信息: “在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常.”,行号为“8”,行位置为“33”. 现象描述: 在处理界面时,界面上增加了一个LISTBOX来处理接收来的数据,单击列表中的其中一项时会弹出详细信息.所以我就在样式

windows server 2008 r2, 每隔一段时间自动关机

前段时间在做Exchange 2010测试的时候,由于windows server 2008  r2试用过期,开机后二个小时就会自动关机,可是我又不想重装系统,加为那样我可能需要重装好多东西,包括DNS.AD等.于是在网上搜索下看看有没有解 决方法,可是找了半天也没找到有用的.于是我只有自己想办法了.我们知道系统是靠slms来检测激活,及开机二个小时关机的.可是这个服务又不能禁用也不 能停用,只能结束进程,可是当把这个进程结束后,系统会在1分钟后关机,我想,能不能取消1分钟后的自动关机呢,试了下

实例对比WPF中的Timer和DispatcherTimer

本文使用Timer和DispatcherTimer制作电子时钟,通过实例对比来了解两者的本质区别. 下面是实例最终的运行画面.其中时钟1使用Timer实现,时钟2使用DispatcherTimer实现. 下面给出完整的实例代码(省略画面代码). using System; using System.Windows; namespace DispatcherTimereExp { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary&g

System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer

System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Threading.Timer!System.Timers.Timer.System.Threading.Timer这两个平时用的时候没有发现太大的区别

System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

一.System.Windows.Forms.Timer 1.基于Windows消息循环,用事件方式触发,在界面线程执行:是使用得比较多的Timer,Timer Start之后定时(按设定的Interval)调用挂接在Tick事件上的EvnetHandler.在这种Timer的EventHandler中可 以直接获取和修改UI元素而不会出现问题--因为这种Timer实际上就是在UI线程自身上进行调用的. 2.它是一个基于Form的计时器3.创建之后,你可以使用Interval设置Tick之间的跨

WPF 添加ACTIVEX 控件 ——System.Windows.Forms.AxHost+InvalidActiveXStateException异常

---恢复内容开始--- 一.ACTIVEX及其相关概念 COM(Component Object Model,对象组件模型): DCOM(Distributed COM,分布式对象组件模型): CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构): 动态数据交换(Dynamic Data Exchange,DDE),解决了数据的更新,但数据格式仍是问题: 对象的链接与嵌入(Object Linking and Embedde

WPF窗体视图中绑定Resources文件中字符串时,抛出:System.Windows.Markup.StaticExtension

问题描述: 在Resources.resx定义了一个静态字符串字段Title,并在WPF窗体视图中绑定为窗体的标题: Title="{x:Static local:Resources.Title}" 但是,在运行应用时,抛出System.Windows.Markup.StaticExtension异常. 原因: 解决方案: 将Resources.resx的访问修饰符由internal修改为public.