基本信息
源码名称:WPF:AsyncDemo(简单异步通信Demo)
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2019-03-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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.Tasks;
using System.Threading;
using System.Windows.Threading;
namespace AsyncDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TextBox aa = new TextBox();
object[] dd = new object[]{};
//方法一
aa.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
{//这里更新你的集合
aa.Text = "";
});
}
private readonly TaskScheduler _syncContextTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
private void button1_Click(object sender, RoutedEventArgs e)
{
TaskThread bb = new TaskThread(textBox1,textBox2,textBox3,_syncContextTaskScheduler);
Task.Factory.StartNew(bb.Begin);
}
private void SchedulerWork()
{
Task.Factory.StartNew(Begin, this.textBox1).Wait();
Task.Factory.StartNew(Begin, this.textBox2).Wait();
Task.Factory.StartNew(Begin, this.textBox3).Wait();
}
private void Begin(object obj)
{
TextBox tb = obj as TextBox;
int i = 100000000;
while (i>0)
{
i--;
}
Random random = new Random();
String Num = random.Next(0,100).ToString();
Task.Factory.StartNew(() => UpdateTb(tb, Num),
new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
}
private void UpdateTb(TextBox tb, string text)
{
tb.Text = text;
}
public class TaskThread
{
private TextBox tb1;
private TextBox tb2;
private TextBox tb3;
private TaskScheduler _syncContextTaskScheduler;
public TaskThread(TextBox tb1, TextBox tb2, TextBox tb3, TaskScheduler _syncContextTaskSchedule)
{
this.tb1 = tb1;
this.tb2 = tb2;
this.tb3 = tb3;
this._syncContextTaskScheduler = _syncContextTaskSchedule;
}
public void Begin()
{
Random random = new Random();
String Num = random.Next(0, 100).ToString();
String Num2 = random.Next(0, 100).ToString();
String Num3 = random.Next(0, 100).ToString();
Task.Factory.StartNew(() => UpdateTb(tb1, Num),new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
Task.Factory.StartNew(() => UpdateTb(tb2, Num2), new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
Task.Factory.StartNew(() => UpdateTb(tb3, Num3), new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
}
private void UpdateTb(TextBox tb, string text)
{
tb.Text = text;
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
/*binding方法直接更新
ViewModel zz = new ViewModel();
this.groupBox2.DataContext = zz;
zz.TextBox1 = "1";
zz.TextBox2 = "2";
zz.TextBox3 = "3";*/
//binding方法异步更新
ViewModel zz = new ViewModel();
this.groupBox2.DataContext = zz;
TaskThread2 bb = new TaskThread2(zz, _syncContextTaskScheduler);
Task.Factory.StartNew(bb.Begin);
}
public class TaskThread2
{
private ViewModel tb1;
private TaskScheduler _syncContextTaskScheduler;
public TaskThread2(ViewModel tb1, TaskScheduler _syncContextTaskSchedule)
{
this.tb1 = tb1;
this._syncContextTaskScheduler = _syncContextTaskSchedule;
}
public void Begin()
{
Task.Factory.StartNew(() => UpdateTb(tb1), new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
}
private void UpdateTb(ViewModel tb)
{
Random random = new Random();
String Num = random.Next(0, 100).ToString();
String Num2 = random.Next(0, 100).ToString();
String Num3 = random.Next(0, 100).ToString();
tb.TextBox1 = Num;
tb.TextBox2 = Num2;
tb.TextBox3 = Num3;
}
}
private void button3_Click(object sender, RoutedEventArgs e)
{
//复杂UI更新
ViewModel2 zz = new ViewModel2();
this.groupBox3.DataContext = zz;
TaskThread3 bb = new TaskThread3(zz);
Task.Factory.StartNew(bb.Begin);
}
public class TaskThread3
{
private ViewModel2 tb1;
public TaskThread3(ViewModel2 tb1)
{
this.tb1 = tb1;
}
public void Begin()
{
Random random = new Random();
String Num = random.Next(0, 100).ToString();
String Num2 = random.Next(0, 100).ToString();
String Num3 = random.Next(0, 100).ToString();
tb1.TextBox1 = Num;
tb1.TextBox2 = Num2;
tb1.TextBox3 = Num3;
}
}
}
}