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(() => ...
随机推荐
- 深度剖析YOLO系列的原理
深度剖析YOLO系列的原理 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/12072225.html 目录 1. ...
- Day01-基础数据类型/用户交互/流程控制之-if
1.基础数据类型 什么是数据类型 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘ ...
- python多线程编程—同步原语入门(锁Lock、信号量(Bounded)Semaphore)
摘录python核心编程 一般的,多线程代码中,总有一些特定的函数或者代码块不希望(或不应该)被多个线程同时执行(比如两个线程运行的顺序发生变化,就可能造成代码的执行轨迹或者行为不相同,或者产生不一致 ...
- 使用VMware安装CentOS 7
环境:Windows10 , VMware Workstation 15 Player, CentOS 7 为什么选择CentOS ? 主流: 目前的Linux操作系统主要应用于生产环境,主流企业级L ...
- Git常用进阶操作之一
提起Git,经常做项目的我们都不陌生,我们常用的功能有哪些呢? 这里按个人使用情况简单总结一下. 像新建远程仓库.新建分支这些就不说了,不熟的同学可以翻看我前面写的git基本操作. 1.首先提一下为每 ...
- DataGridView使用自定义控件实现简单分页功能
本例子使用自定义控件方法实现,数据库使用的是SQL Server,实现过程如下: 1.新建一个自定义控件,命名为:PageControl. 2.PageControl代码如下: public part ...
- centos7 laravel 项目 npm install报错
npm install 初始化项目依赖的前端资源 报错 ERR xxx .. socket,symbol link is not supported ... 如果报错了 重新npm install ...
- Spring Boot 2.2.2.RELEASE 版本中文参考文档【3.1】
使用Spring Boot 本节将详细介绍如何使用Spring Boot.它涵盖了诸如构建系统,自动配置以及如何运行应用程序之类的主题.我们还将介绍一些Spring Boot最佳实践.尽管Spring ...
- My97DatePicker-WdatePicker日历日期插件详细示例
<!DOCTYPE html> <html> <head> <title>排行</title> <meta charset=" ...
- fiddler教程:抓包带锁的怎么办?HTTPS抓包介绍。
点击上方↑↑↑蓝字[协议分析与还原]关注我们 " 介绍Fiddler的HTTPS抓包功能." 这里首先回答下标题中的疑问,fiddler抓包带锁的原因是HTTPS流量抓包功能开启, ...