调度员如何工作一个线程? [英] How does dispatcher work the a thread?

查看:62
本文介绍了调度员如何工作一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用调度程序的Silverlight应用程序,请您帮忙解释以下代码的作用? (不幸的是,编写代码的开发人员已经离开了.)

We have a silverlight application which uses a dispatcher and I would appreciate any help explaining what the following codes does? (unfortunately the developer who wrote the code has left).

所以我们有以下内容:

public class ABC
{
    private Dispatcher dispatcher;
    private Thread threadRunner;

    public void ABC()
    {
       threadRunner= new Thread(ThreadRunnerMethod)
                         {
                           IsBackground = true, 
                           ApartmentState = ApartmentState.STA
                         };
       threadRunner.Start();
    }

    private static void ThreadRunnerMethod()
    {
       Dispatcher.Run();
    }


    public void MainMethod()
    {
       dispatcher = Dispatcher.FromThread(threadRunner);
       dispatcher.Invoke(new Action(() => 
                                     // "DO SOME WORK WITH A COM OBJECT"
                                      ));

    }
}

我对线程有一些基本的经验,但是我不知道这一切如何工作?

I have some basic experience with threading but I have no idea how this all works?

JD

推荐答案

基本上,这等效于Control.Invoke在Windows Forms中的调用-只是被分成了自己的对象.

It's the equivalent of Control.Invoke in Windows Forms, basically - it's just been separated into its own object.

据我了解,Dispatcher.Run基本上将启动事件循环,您可以使用Dispatcher.Invoke将调用编组到该事件循环中. Dispatcher.FromThread找到负责给定线程的Dispatcher对象-因此,在这种情况下,它找到在新线程中运行的事件循环.

As I understand it, Dispatcher.Run will basically start an event loop, and you can marshall calls into that event loop using Dispatcher.Invoke. Dispatcher.FromThread finds the Dispatcher object which is responsible for a given thread - so in this case, it finds the event loop running in the new thread.

因此,在您的代码中,使用lambda表达式创建的委托将在新创建的线程中执行.

So in your code, the delegate created with the lambda expression will execute in the newly created thread.

这篇关于调度员如何工作一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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