Async in WPF

This is a just simple summary about Async in WPF

  1. UI Thread will lock all the ViewModel which is used by it. So, any thread try to access to the ViewModel will throw an exception.
    But Function can be shared by other thread.

Reminder: if any functions or event_hanlder will operate the UI Control, we had better not call them in a non-UI thread. If we really need to do this job This function can help Application.Current.Dispathcer.Invoke(Action).

  1. If we define some functions which is time consuming, and we need to execute them in a seperate thread, it is a good pratice to pass all the variables through parameters. We should not suppose that, we can access the objects defined in the ViewModel.

  2. A simple summary:
    3.1 UI Thread have root right, It can access to any objects. and it will lock the objects which is used by itself. if any other thread hope to access to it, they may need to call Application.Current.Dispatcher.Invoke(Action).
    3.2 aysnc function should be stand alone, all the data is through parameter.
    3.3 all ui controls access need the help of Application.Current.Dispatcher.Invoke(Action).

posted @   kongshu  阅读(141)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示