WPF 的 await Application.Current.Dispatcher.InvokeAsync,Func 如果是Task , 等待赋值可能存在没有等待执行完成的问题
最近在检查我们组内的代码,发现好多用到
await Application.Current.Dispatcher.InvokeAsync
相信好多WPF的开发都会用到 该方法做UI线程切换。但是细看里边的执行,发现了个别存在存在没有等待完成的情况


执行的结果是 i 为 null 值。
这也就说明了 执行了
await Application.Current.Dispatcher.InvokeAsync
,其实还没有完成等待完成,AI提示返回的是 DispatcherOperation<Task> 的 返回结果
反编译导航进去

对于执行 Func<T> callback 回调的,返回的是DispacherOperation<Func<Task>>
也就是说,要正在执行完成该函数,需要在 await Task<T> ,才能 拿到TResult 的结果
所以,解决的方法如下:
await await Application.Current.Dispatcher.InvokeAsync(Func<Task>)

执行的结果:

WPF 的 await Application.Current.Dispatcher.InvokeAsync,Func 如果是Task , 等待赋值可能存在没有等待执行完成的问题的更多相关文章
- WPF 的 Application.Current.Dispatcher 中,为什么 Current 可能为 null
原文:WPF 的 Application.Current.Dispatcher 中,为什么 Current 可能为 null 在 WPF 程序中,可能会存在 Application.Current.D ...
- WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null
原文:WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null 在 WPF 程序中,可能会存在 Application.Curren ...
- System.Windows.Application.Current.Dispatcher.BeginInvoke
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => ...
- WPF入门教程系列四——Dispatcher介绍
一.Dispatcher介绍 微软在WPF引入了Dispatcher,那么这个Dispatcher的主要作用是什么呢? 不管是WinForm应用程序还是WPF应用程序,实际上都是一个进程,一个进程可以 ...
- win10 uwp Window.Current.Dispatcher中Current为null
本文说的是进行网络中异步界面出现的错误,可能带有一定的主观性和局限性,说的东西可能不对或者不符合每个人的预期.如果觉得我有讲的不对的,就多多包含,或者直接关掉这篇文章,但是请勿生气或者发怒吐槽,可以在 ...
- 2018-8-10-win10-uwp-Window.Current.Dispatcher中Current为null
title author date CreateTime categories win10 uwp Window.Current.Dispatcher中Current为null lindexi 201 ...
- WPF中Application.Current的使用
WPF程序对应一个Application对象,当前的Application对象可以通过Application.Current获取,通过获取到的Application对象,我们可以做以下的事情: App ...
- Application.Current的使用
来源 http://www.cnblogs.com/symons/archive/2010/03/15/1686200.html Application.Current的使用 WPF程序对应一个App ...
- WPF中的Application类。
原文:WPF中的Application类. Application对象用的名称空间是system.windows 1.手动创建Application对象步骤. 1.1).把项目中的App.Xaml文件 ...
- A candidate solution for Java Web Application - current session
Motivation Do it once, resue for ever. Audience myself, Java Web developers Scope 应用案例 图书借阅系统 阶段1需求: ...
随机推荐
- TDH - 如何显示Guardian Client角色
注意:本博客适用TDH版本4.3.x 默认该页面的 Guardian Client 是隐藏的,如果需要对 Guardian Client角色进行什么操作的话,需要先将 Guardian Client角 ...
- ABC393C题解
大概评级:橙. 送分题. 题意就是让你统计有多少条边是重边或自环. 设 \(u_i\) 表示第 \(i\) 条边的左端点,\(v_i\) 表示第 \(i\) 条边的右端点. 那么如果 \(u_i = ...
- [解决方案]Refusing to install package with name "codemirror" under a package
前言 安装codeMirror,报错了 报错信息:Refusing to install package with name "codemirror" under a packag ...
- C# .net 压缩文件解压上传及文件压缩下载
using ICSharpCode.SharpZipLib.Zip; using System; using System.Collections.Generic; using System.IO; ...
- 探秘Transformer系列之(14)--- 残差网络和归一化
探秘Transformer系列之(14)--- 残差网络和归一化 目录 探秘Transformer系列之(14)--- 残差网络和归一化 0x00 概述 0x01 残差连接 1.1 问题 1.2 相关 ...
- docker push image harbor http 镜像
前言 搭建的 harbor 仓库为 http 协议,在本地登录后,推送镜像发生如下报错: docker push 192.168.xx.xx/test/grafana:v10.1.1 The push ...
- ant design pro git提交error; Angular 团队git提交规范
前言 在使用 ant design pro 时,git 提交报错 > running commit-msg hook: fabric verify-commit ERROR 提交日志不符合规范 ...
- Django实战项目-学习任务系统-发送邮件通知
接着上期代码内容,继续完善优化系统功能. 本次增加发送邮件通知功能,学习任务系统发布的任务,需要及时通知到学生用户知晓. 由于目前智能手机普及,人人都离不开手机,所以手机端接收通知信息更加及时有效. ...
- Linux中查看进程状态信息
一.常用命令总结 ps -l 列出与本次登录有关的进程信息: ps -aux 查询内存中进程信息: ps -aux | grep *** 查询***进程的详细信息: t ...
- Vim 操作-替换
Vim 操作-替换 substitute :[range]s[ubstitute]/{pattern}/{string}/{flag} 替换的操作范围以行为基础: %-全局范围,m,n-使用逗号隔开的 ...