首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UI线程上的Silverlight WCF数据服务回调返回

UI线程上的Silverlight WCF数据服务回调返回
EN

Stack Overflow用户
提问于 2013-11-05 18:44:44
回答 1查看 219关注 0票数 2

我在WCF data service中使用Silverlight 5,并且我已经安装了Silverlight WCF data Service客户端库。

理想情况下,当我执行DataServiceQuery<T>.BeginExecute时,回调应该在工作线程上返回,但在我的例子中,它在UI线程上返回。

我担心的是我的负载非常大,大约需要6-7秒来实现对对象的响应(DataServiceQuery<T>.EndExecute method)。因此,如果它在UI线程上运行,它会阻塞我的UI 6-7秒。

如何获取工作线程上的响应,物化工作线程上的响应,然后切换到UI线程来更新我的UI。

有没有人遇到过类似的问题?

这是我的代码。

代码语言:javascript
运行
复制
private void getRealTimeAssetProfileQuery(Guid assetProfileID, DateTime fromDate, DateTime toDate)
{
    dateRanges[assetProfileID].Add(new Range<DateTime>(fromDate, toDate));

    context = ServiceAgent.Context;
    var qry = (from o in context.RealTimeProfilePoint
               where o.ProfileID == assetProfileID && o.PointTime >= fromDate && o.PointTime <= toDate
               orderby o.PointTime descending
               select o) as DataServiceQuery<RealTimeProfilePoint>;

    qry = qry.AddQueryOption("$expand", "Profile");
    qry = qry.AddQueryOption("$expand", "Profile/ProfileType");


    dynamic asyncState = new
    {
        assetProfileID = assetProfileID,
        fromDate = fromDate,
        toDate = toDate
    };


    Uri qryUri = new Uri(qry.ToString());

    context.BeginExecute<RealTimeProfilePoint>(qryUri, new AsyncCallback(realTimeCallBack), asyncState);
}

void realTimeCallBack(IAsyncResult asyncResult)
{
    if (Deployment.Current.Dispatcher.CheckAccess())
        raiseTrace("Data Service Response On UI Thread \n");
    else
        raiseTrace("Data Service Response On worker Thread \n");


    dynamic asyncState = asyncResult.AsyncState as dynamic;

    Guid assetProfileID = asyncState.assetProfileID;
    DateTime fromDate = asyncState.fromDate;
    DateTime toDate = asyncState.toDate;

    IEnumerable<RealTimeProfilePoint> profilePoints = context.EndExecute<RealTimeProfilePoint>(asyncResult);

    List<RealTimeProfilePoint> lstProfilePoint = profilePoints.ToList();

    if (((QueryOperationResponse)profilePoints).GetContinuation() != null)
    {
        Uri qryUri = ((QueryOperationResponse)profilePoints).GetContinuation().NextLinkUri;

        context.BeginExecute<RealTimeProfilePoint>(qryUri, new AsyncCallback(realTimeCallBack), asyncState);
    }

    if (lstProfilePoint.Count > 0)
    {
        var assetProfile = lstProfilePoint[0].Profile;

        Instance.addRealTimePoints(assetProfile, lstProfilePoint);

        raiseTrace(
               "Fetching Real Time Profile Points from Server for AssetProfileID " +
               assetProfileID.ToString() + " : " +
               lstProfilePoint.Count.ToString() + " record(s) found for" +
               fromDate.ToUtcTime().ToString() + " - " + toDate.ToUtcTime().ToString() +
               " peiod.");
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-01-31 22:27:04

从(后台)线程启动您的请求。

代码语言:javascript
运行
复制
 dynamic asyncState = new
 {
     assetProfileID = assetProfileID,
     fromDate = fromDate,
     toDate = toDate,

     uiDispatcher = Application.Current.MainWindow.Dispatcher, // for gui access on background thread
 };

 new Thread(() =>
 {
     context.BeginExecute<RealTimeProfilePoint>(qryUri, new AsyncCallback(realTimeCallBack), asyncState);
 })
 {
     IsBackground = true,
 }
 .Start();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19787105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档