如何判断当前线程是否为 UI 线程? [英] How do I tell if my current thread is the UI thread?

查看:146
本文介绍了如何判断当前线程是否为 UI 线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 UWP 开发一个用户控件,它会在某些调用时更新它的一些视觉效果.但是,由于核心.NET库已经转移并且线程类被严重削减,我不知道如何从用户控件中的方法中识别调用线程是否为UI线程所以它知道是否使用调度器来改变依赖属性.

I'm working on a user control for UWP and it updates some of its visuals upon certain calls. However, since the core .NET library has been shifted around and the threading classes have been severely cut back, I don't know how to identify from the method in the user control if the calling thread is the UI thread or not so it knows whether or not to use the dispatcher to change a dependency property.

有人知道怎么做吗?

调度程序功能上可以在 UI 线程上调用"异步罚款 - 但是,我真的不知道这是否是一个好主意 - 尝试通过调度程序从主线程调用主线程.如果有人有理由认为这是好是坏,那也有助于回答这个问题.如果没有理由不在主线程上使用dispatcher,那我想应该没问题.

The dispatcher functionally can be "invoked" async fine on the UI thread - however, I really don't know if this is a good idea - to try to invoke on the main thread from the main thread via the dispatcher. If anyone has a reason this is fine or bad, that would also contribute to answering the question. If there's no reason to not use the dispatcher on the main thread, then I guess there's no problem.

推荐答案

我找到了解决方案...

I have found the solution...

CoreDispatcher.HasThreadAccess 返回一个布尔值,表示如果您在 UI 线程上.

CoreDispatcher.HasThreadAccess returns a bool indicating if you are on the UI thread or not.

这篇关于如何判断当前线程是否为 UI 线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆