吴佳鑫的个人专栏

当日事当日毕,没有任何借口

导航

silverlight中的多线程和计时器

多线程

XAML代码:

<UserControl x:Class="Sample.ThreadSample"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Width
="350" Height="180">
<StackPanel x:Name="LayoutRoot">
<Border x:Name="border" Background="AliceBlue" Margin="5"
BorderBrush
="Black" BorderThickness="3" CornerRadius="5">
</Border>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnStart" Width="100" Height="30"
Content
="开始线程"
Click
="btnStart_Click" Margin="10"></Button>
<Button x:Name="btnJoin" Width="100" Height="30"
Content
="线程延迟"
Click
="btnJoin_Click" Margin="10"></Button>
</StackPanel>
</StackPanel>
</UserControl>

后台代码:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using System.Threading;
13
14 namespace Sample
15 {
16 public partial class ThreadSample : UserControl
17 {
18 private static System.Windows.Controls.TextBlock tbk;
19 private Thread newThread;
20 public ThreadSample()
21 {
22 InitializeComponent();
23 //将文本控件添加到界面上
24 tbk = new TextBlock()
25 {
26 FontSize = 24
27 ,
28 Width = 300
29 ,
30 Height = 100
31 };
32 border.Child = tbk;
33 //创建一个新的线程并执行静态方法
34 newThread = new Thread(ThreadSample.SetText);
35 //线程开始
36 }
37
38 public static void SetText()
39 {
40 int i = 60;
41 while (i > 0)
42 {
43 tbk.Dispatcher.BeginInvoke(delegate()
44 {
45 tbk.Text = "离线程结束还有" + i + "";
46 });
47 i--;
48 //线程等待
49 Thread.Sleep(1000);
50 }
51 }
52
53 private void btnStart_Click(object sender, RoutedEventArgs e)
54 {
55 newThread.Start();
56 }
57
58 private void btnJoin_Click(object sender, RoutedEventArgs e)
59 {
60 newThread.Join(2000);
61 }
62 }
63 }

 

 

DispatcherTimer 是SL中很有用的一个计时器对象。也是基础SL 的UI线程

XAML代码:

<UserControl x:Class="Sample.Timer"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Width
="400" Height="200">
<Grid x:Name="LayoutRoot" Background="White">
<!--背景-->
<Rectangle Fill="Gold" Stroke="Black"
StrokeThickness
="3"
RadiusX
="5" RadiusY="5"/>
<!--显示时间-->
<TextBlock x:Name="tbkTimer"
Width
="300" Height="50"
FontSize
="30" Foreground="Red"/>
</Grid>
</UserControl>

 

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Sample
{
public partial class Timer : UserControl
{
public Timer()
{
InitializeComponent();
//创建DispatcherTimer
DispatcherTimer timer = new DispatcherTimer();
//设置间隔1秒
timer.Interval = new TimeSpan(0, 0, 1);
//创建事件处理
timer.Tick += new EventHandler(timer_Tick);
//开始计时
timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
//输出时间
tbkTimer.Text = "当前时间:" + DateTime.Now.ToLongTimeString();
}
}
}

posted on 2011-08-30 22:59  _eagle  阅读(541)  评论(0编辑  收藏  举报