.NET创建新的分派器 [英] .NET Create New Dispatcher

查看:64
本文介绍了.NET创建新的分派器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用调度程序创建第二个线程,以便使主调度程序(用于UI)完全没有压力,并使UI始终保持响应状态.

I am trying to create a second thread with dispatcher so that I can have the primary dispatcher (for the UI) completely stress-free, and have the UI constantly respondant.

现在,我可以为每个子创建多个线程(或在C#中为空),但是我是否有可能创建一个新线程并抓住它的调度程序并对其进行调用?这就是我所做的:

Now, I could create multiple threads for each sub (or void in C#), but isn't it possible for me to create one new thread and grab it's dispatcher, and invoke to that? This is what I've done:

Private CheckLoopThread As New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf CheckLoop))

CheckLoopThread.Priority = System.Threading.ThreadPriority.Lowest
CheckLoopThread.Start()
Dim Test As Windows.Threading.Dispatcher = Windows.Threading.Dispatcher.FromThread(CheckLoopThread)

但是,变量"Test"在执行"Nothing"之后.这怎么可能?是创建第二个调度程序的另一种方法吗?

However, the variable "Test" is after execution "Nothing". How is this possible? Is the another way to create a second dispatcher?

以任何.NET形式对答案表示赞赏.Visual Basic或C#.我正在.NET 4.0框架的VB.NET WPF中工作.

Answers are appreciated in any .NET form. Visual Basic or C#. I am working in VB.NET WPF on the .NET 4.0 framework.

提前谢谢.

推荐答案

Dispatcher.FromThread(...) 将不会创建Dispatcher,如果尚未为该线程创建Dispatcher,则将返回null.若要为线程创建分派器,您将必须访问

Dispatcher.FromThread(...) will not create a Dispatcher and will return null if a Dispatcher has not already been created for the thread. To create a Dispatcher for a thread, you will have to access Dispatcher.CurrentDispatcher at least once on your CheckLoopThread. As it says on MSDN for Dispatcher.CurrentDispatcher:

如果Dispatcher与当前线程不相关,则新将创建分派器.FromThread并非如此方法.如果没有调度程序,则FromThread将返回null与指定线程相关联

If a Dispatcher is not associated with the current thread, a new Dispatcher will be created. This is not the case with the FromThread method. FromThread will return null if there is not a dispatcher associated with the specified thread

这篇关于.NET创建新的分派器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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