【问题标题】:Silverlight WCF data service call back returns on UI threadSilverlight WCF 数据服务回调在 UI 线程上返回
【发布时间】:2013-11-16 05:10:43
【问题描述】:

我正在使用Silverlight 5WCF data service,并且我已经安装了 Silverlight WCF 数据服务客户端库。

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

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

如何在工作线程上获得响应,在工作线程上实现响应,然后切换到 UI 线程来更新我的 UI。

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

这是我的代码。

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.");
    }
}

【问题讨论】:

    标签: multithreading silverlight mvvm wcf-data-services silverlight-5.0


    【解决方案1】:

    从(后台)线程开始您的请求。

     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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多