Wpf Dispatcher.BeginInvoke((Action)delegate{}));
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Command="{Binding ClickCmd}" Content="Click Cmd"/>
<Button Grid.Row="0" Grid.Column="1" Command="{Binding CancelCmd}" Content="Cancel Cmd"/>
<ListBox Grid.Row="1" BorderBrush="Black" BorderThickness="3" Grid.RowSpan="2" ItemsSource="{Binding ContentOb,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input; namespace WpfApplication1.Model
{
public class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute; public event EventHandler CanExecuteChanged; public DelegateCommand(Action<object> execute)
: this(execute, null)
{
} public DelegateCommand(Action<object> execute,
Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
} public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
} return _canExecute(parameter);
} public void Execute(object parameter)
{
_execute(parameter);
} public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using WpfApplication1.Model;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using System.IO;
using Newtonsoft.Json; namespace WpfApplication1.ViewModel
{
public class WpfVM : INotifyPropertyChanged
{
private WpfVM()
{ }
public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if(handler!=null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propName));
}
} private static WpfVM Wpfvm;
private static readonly object objLock = new object();
public static WpfVM GetWpfVM()
{
lock(objLock)
{
if(Wpfvm == null)
{
Wpfvm = new WpfVM();
}
return Wpfvm;
}
} private static CancellationTokenSource cts=new CancellationTokenSource(); #region Commands
private DelegateCommand ClickCmdValue;
public DelegateCommand ClickCmd
{
get
{
if(ClickCmdValue==null)
{
ClickCmdValue = new DelegateCommand(ClickCmdExecuted);
}
return ClickCmdValue;
}
} private string ContentValue;
public string Content
{
get
{
return ContentValue;
}
set
{
if(value!=ContentValue)
{
ContentValue = value;
OnPropertyChanged("Content");
}
}
} private bool IsCancelledValue=false;
public bool IsCancelled
{
get
{
return IsCancelledValue;
}
set
{
if(value!=IsCancelledValue)
{
IsCancelledValue = value;
OnPropertyChanged("IsCancelled");
}
}
}
private void ClickCmdExecuted(object obj)
{
ContentOb = new ObservableCollection<string>();
Task.Run(() =>
{
while (!cts.IsCancellationRequested)
{
Content = DateTime.Now.ToString("yyyyMMddHHmmssffff");
App.Current.Dispatcher.BeginInvoke((Action)delegate
{
ContentOb.Add(Content);
});
System.Diagnostics.Debug.WriteLine(Content);
}
},cts.Token);
} private DelegateCommand CancelCmdValue;
public DelegateCommand CancelCmd
{
get
{
if(CancelCmdValue==null)
{
CancelCmdValue = new DelegateCommand(CancelCmdValueExecuted);
}
return CancelCmdValue;
}
} private void CancelCmdValueExecuted(object obj)
{
cts.Cancel();
IsCancelled = true;
System.Diagnostics.Debug.WriteLine("Cancelled!");
string msJsonSerializerString = JsonConvert.SerializeObject(ContentOb,Formatting.Indented);
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".txt";
using(StreamWriter streamWriter=new StreamWriter(fileName,true,Encoding.UTF8))
{
streamWriter.WriteLine(msJsonSerializerString);
}
}
#endregion #region Properties
private ObservableCollection<string> ContentObValue;
public ObservableCollection<string> ContentOb
{
get
{
return ContentObValue;
}
set
{
if(value!=ContentObValue)
{
ContentObValue = value;
OnPropertyChanged("ContentOb");
}
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApplication1.ViewModel; namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WpfVM vm = WpfVM.GetWpfVM();
this.DataContext = vm;
}
}
}
private void ClickCmdExecuted(object obj)
{
ContentOb = new ObservableCollection<string>();
Task.Run(() =>
{
while (!cts.IsCancellationRequested)
{
Content = DateTime.Now.ToString("yyyyMMddHHmmssffff");
App.Current.Dispatcher.BeginInvoke((Action)delegate
{
ContentOb.Add(Content);
});
System.Diagnostics.Debug.WriteLine(Content);
}
},cts.Token);
}
Wpf Dispatcher.BeginInvoke((Action)delegate{}));的更多相关文章
- WPF Dispatcher.BeginInvoke子线程更新UI
在开发WPF应用时出现:”调用线程无法访问此对象,因为另一个线程拥有该对象.“ 是因为UI线程是WPF应用的主线程,若尝试子线程更新UI线程应使用Dispatcher.BeginInvoke()或者I ...
- Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析
原文:Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析 前段时间,公司同事开发了一个小工具,在工具执行过程中,UI界面一直处于卡死状态. 通过阅读代码发现,主要是 ...
- WPF子线程更新UI(Dispatcher.BeginInvoke)
在做WPF开发时,如果直接在子线程里更新UI会报错—–“调用线程无法访问此对象,因为另一个线程拥有该对象.”,这是因为WPF禁止在非UI线程里直接更新UI界面. 解决方案: 在子线程里调用D ...
- WPF Dispatcher 一次小重构
几个月之前因为项目需要,需要实现一个类似于WPF Dispatcher类的类,来实现一些线程的调度.之前因为一直做Asp.Net,根本没有钻到这个层次去,做的过程中,诸多不顺,重构了四五次,终于实现, ...
- 深入了解 WPF Dispatcher 的工作原理(Invoke/InvokeAsync 部分)
深耕 WPF 开发的各位程序员大大们一定避不开使用 Dispatcher.跨线程访问 UI 当然免不了用到它,将某个任务延迟到当前任务之后执行也会用到它.Dispatcher.Invoke.Dispa ...
- WPF Dispatcher的使用
<Window x:Class="DispatcherExam.MainWindow" xmlns="http://schemas.micro ...
- WPF Dispatcher介绍
微软在WPF引入了Dispatcher,那么这个Dispatcher的主要作用是什么呢?Dispatcher的作用是用于管理线程工作项队列.主线程负责接收输入.处理事件.绘制屏幕等工作,这样一来,UI ...
- WPF Dispatcher使用
微软在WPF引入了Dispatcher,那么这个Dispatcher的主要作用是什么呢?Dispatcher的作用是用于管理线程工作项队列.主线程负责接收输入.处理事件.绘制屏幕等工作,这样一来,UI ...
- System.Windows.Application.Current.Dispatcher.BeginInvoke
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => ...
随机推荐
- prototype原型
1.prototype是函数的一个属性,并且是函数的原型对象.引用它的必然是函数[对象都是通过函数创建的], 这个prototype的属性值是一个对象(属性的集合,再次强调!),默认的只有一个叫做co ...
- 用js写直角三角形,等腰三角形,菱形
//一. 画一个直角三角形 // 第几行 *号数 // * 1 1 // ** 2 2 // *** ...
- 《Java基础知识》Java IO流详解
Java IO概念 1. 用于设备之间的数据传输. 2. Java 将操作数据流的功能封装到了IO包中. 3. 数据流流向分:输入流和输出流,操作对象为文件. 4. 流按照操作数据分:字节流(通用)和 ...
- web前端分享JavaScript到底是什么?特点有哪些?
web前端分享JavaScript到底是什么?特点有哪些?这也是成为web前端工程师必学的内容.今天为大家分享了这篇关于JavaScript的文章,我们一起来看看. 一.JavaScript是什么? ...
- JS---案例:筋斗云
案例:筋斗云 鼠标进入,一朵云的样式跟随鼠标移动,鼠标点击后离开,云样式回到上次点击的位置 <!DOCTYPE html> <html lang="en"> ...
- 微服务与敏捷开发(Scrum/Kanban)的核心思想之我见
微服务与敏捷开发(Scrum/Kanban)的核心思想之我见 关于"微服务"和"敏捷开发"的文章网络上有很多,所以这里不再重复叙述这些概念的解释和特点,而是 ...
- RF之简介
robot framework 是一个通用型的自动测试框架 - 自动测试用例的实现方式 - 自动测试用例的开发支持 : IDE.库 - 和用例管理系统的集成 - 测试执行:相关测试套件和测试用例的执 ...
- React劲爆新特性Hooks 重构去哪儿网火车票PWA
React劲爆新特性Hooks 重构去哪儿网火车票PWA 获取课程资料链接:点击这里获取 本课程先带你细数最近一年来React的新特性,如Hooks.Redux API,让你从头理解Hooks对传统R ...
- 前端小白webpack学习(四)
.less文件与.scss文件使用与.css文件相仿 less-loader使用需要借助less插件,终端输入npm i less less-loader -D安装; sass-loader使用需要借 ...
- IT兄弟连 HTML5教程 CSS3属性特效 动画-animation
CSS3属性中有关于制作动画的三个属性:Transform,Transition,Animation.前面已经介绍过Transform和Transition了,这里我们来学习Animation动画.通 ...