2

我正在使用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.");
    }
}
4

1 回答 1

0

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

 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();
于 2014-01-31T14:27:04.343 回答