我正在尝试使用调度程序创建第二个线程,以便我可以让主调度程序(用于 UI)完全无压力,并且让 UI 不断响应。
现在,我可以为每个子线程(或 C# 中的 void)创建多个线程,但我是否有可能创建一个新线程并获取它的调度程序,然后调用它?这就是我所做的:
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”。这怎么可能?是创建第二个调度程序的另一种方法吗?
欢迎以任何 .NET 形式提供答案。 Visual Basic 或 C#。我正在 .NET 4.0 框架上使用 VB.NET WPF。
提前致谢。
为什么要锁?
我更喜欢:
Dispatcher myDispatcher = null;
// create a manual reset event for crossthread signalling.
var dispatcherReadyEvent = new ManualResetEvent(false);
// create a new thread.
new Thread(new ThreadStart(() =>
{
// get the current dispatcher (if it didn't exists
// it will be created.
myDispatcher = Dispatcher.CurrentDispatcher;
// set the signal that the dispatcher is created.
dispatcherReadyEvent.Set();
// run the dispatcher.
Dispatcher.Run();
})).Start();
// wait until the dispatcher is created on the thread.
dispatcherReadyEvent.WaitOne();
// run something on the dispatcher thread.
myDispatcher.Invoke(...);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)