一、创建多线程
<StackPanel x:name="LayoutRoot" Background="White"> <border x:name="border" Background="AliceBlue" margin="5" borderBrush="Black" WIDth="300" borderThickness="3" CornerRadius="5" /> <StackPanel OrIEntation="Horizontal" WIDth="310"> <button x:name="btn_star" WIDth="100" Height="30" Click="btn_star_Click" margin="10" Content="开始倒计时" /> <button x:name="btn_Json" WIDth="100" Click="btn_Json_Click" margin="10" Content="延迟线程" /> </StackPanel> </StackPanel>
C#:
private static TextBlock txb; private Thread newThread; public dispatcherTimerAndThread() { InitializeComponent(); //将文本txb添加到界面上 txb = new TextBlock() { WIDth = 300,Height = 100,FontSize = 24 }; border.Child = txb; //创建一个线程并执行线程方法 newThread = new Thread(dispatcherTimerAndThread.SetText); } public static voID SetText() { int i = 60; while (i > 0) { txb.dispatcher.BeginInvoke(delegate() { txb.Text = "距离线程结束还有:" + i + "秒"; }); i--; Thread.Sleep(1000); } } private voID btn_star_Click(object sender,RoutedEventArgs e) { newThread.Start(); } private voID btn_Json_Click(object sender,RoutedEventArgs e) { newThread.Join(2000); }
按F5运行:
二、利用dispatcherTimer做一个计时器
<GrID x:name="LayoutRoot" Background="White"> <Rectangle Fill="Gold" strokeThickness="3" WIDth="300" Height="150" margin="0,20,0" stroke="Black" RadiusX="5" RadiusY="5"> </Rectangle> <TextBlock x:name="txb_times" WIDth="300" Height="50" FontSize="30" Foreground="Red"/> </GrID>
C#:
public timer() { InitializeComponent(); //创建dispatcherTimer对象 dispatcherTimer timers = new dispatcherTimer(); //设置间隔时间 timers.Interval = new TimeSpan(0,1); //创建处理事件 timers.Tick += new EventHandler(timers_Tick); //开始 timers.Start(); } voID timers_Tick(object sender,EventArgs e) { //当前时间 txb_times.Text = "当前时间:" + DateTime.Now.TolongTimeString(); }
按F5运行:
以上是内存溢出为你收集整理的Silverlight之dispatcherTimer 与 线程全部内容,希望文章能够帮你解决Silverlight之dispatcherTimer 与 线程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)