【问题标题】:Which thread is the callback executing on when performing an asynchronous RIA Services call?执行异步 RIA 服务调用时,回调在哪个线程上执行?
【发布时间】:2011-10-10 10:51:25
【问题描述】:

我在 Silverlight 4 应用程序中使用 RIA 服务 DomainContext 来加载数据。如果我使用 UI 线程的上下文,回调是否总是在 UI 线程上?

或者换一种说法,回调总是和调用在同一个线程上吗?

下面的一些示例代码说明了该场景...

    private void LoadStuff()
    {
        MyDomainContext context = new MyDomainContext ();
        context.Load(context.GetStuffQuery(), op =>
        {
            if (!op.HasError)
            {
                // Use data.

                // Which thread am I on?
            }
            else
            {
                op.MarkErrorAsHandled();

                // Do error handling

            }
        }, null
        );
    }

【问题讨论】:

    标签: c# multithreading silverlight silverlight-4.0 wcf-ria-services


    【解决方案1】:

    如果你在 UI-Thread 上执行 DomainContext 的 Load-Method,那么回调是否也在 UI-Thread 上执行。

    当您使用 Load 返回的 LoadOperation 的 Completed-Event 时也是如此。

    LoadOperation<Stuff> operation = context.Load(context.GetStuffQuery());
    operation.Completed += (o, e) {
      if (!operation.HasError) {
        // Use data.
    
        // Which thread am I on?
      }
      else {
        op.MarkErrorAsHandled();
        // Do error handling
      }
    };
    

    【讨论】:

    • 感谢 Jehof,知道这很有用,这也是我在实践中发现的。不过,我很想对我的具体问题给出明确的答案——是否总是调用线程获得回调? (例如,如果我们不在 UI 线程上调用)。
    • @Chris:是的,回调在调用线程中执行。加载在后台线程中完成,并且对于调用线程是非阻塞的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2021-10-18
    • 1970-01-01
    相关资源
    最近更新 更多