码迷,mamicode.com
首页 > 编程语言 > 详细

D31_01_多线程

时间:2014-11-07 07:33:52      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   div   

bubuko.com,布布扣

 

 

<Window x:Class="demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <StackPanel Margin="5">
        <TextBox Name="txt">Text in a text box.</TextBox>
        <Button Click="cmdBreakRules_Click">Break the rules</Button>
        <Button Click="cmdFollowRules_Click">Follow the rules</Button>
    </StackPanel>
</Window>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void UpdateTextWrong()
        {
            Thread.Sleep(TimeSpan.FromSeconds(3));
            txt.Text = "Here is some new text.";
        }

        private void cmdFollowRules_Click(object sender, RoutedEventArgs e)
        {
            Thread thread=new Thread(UpdateTextRight);
            thread.Start();
        }

        private void UpdateTextRight()
        {
            Thread.Sleep(TimeSpan.FromSeconds(3));

            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()
            {
                UpdateTextWrong();
            });
            //this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()
            //{
            //    //Thread.Sleep(TimeSpan.FromSeconds(3));耗时代码不能写在这里;这里用的是匿名委托
            //    txt.Text = "Here is some new text.";
            //});
        }


        private void cmdBreakRules_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(UpdateTextWrong);
            thread.Start();
        }
    }
}

D31_01_多线程

标签:style   blog   http   io   color   ar   os   sp   div   

原文地址:http://www.cnblogs.com/xiepengtest/p/4080372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!