首页 > 代码库 > D31_01_多线程
D31_01_多线程
<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_多线程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。