• Silverlight之dispatcherTimer 与 线程


     一、创建多线程

     <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,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, 0, 1);
                //创建处理事件
                timers.Tick += new EventHandler(timers_Tick);
                //开始
                timers.Start();
            }
    
            void timers_Tick(object sender, EventArgs e)
            {
               //当前时间
                txb_times.Text = "当前时间:" + DateTime.Now.ToLongTimeString();
            }


    按F5运行:

  • 相关阅读:
    jquery实现页面的搜索功能
    url中的查询字符串的参数解析
    5.14日学习内容1:jquery表单相关知识
    5.12日北京“咖啡陪你”咖啡吧学习笔记
    layui基础上的tree菜单动态渲染;
    H5area的热区锚点随着图片的尺寸而变化
    Python3基础 raise 产生RuntimeError 异常
    Python3基础 raise + 指定类型异常+异常的解释 产生特定类型异常
    Python3基础 判断变量大于一个数并且小于另外一个数
    Python3基础 内嵌函数 简单示例
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5114910.html
Copyright © 2020-2023  润新知