multithreading - Silverlight WCF数据服务回调在UI线程上返回

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

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

最佳答案

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

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

关于multithreading - Silverlight WCF数据服务回调在UI线程上返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19787105/

相关文章:

java - 在多线程 Web 应用程序中访问请求范围的 beans

java - Java 中的 MVVM 模式

c# - WP7从墓碑中恢复并返回页面

java - 通过 Java 网站部署 silverlight 控件

c# - MVVM WPF 中的数据绑定(bind)

java线程中断,线程为空

Java 线程 : Given program isn't behaving properly

windows - WinHttp : can it be used in parallel?

Silverlight MVVM ListBoxItem IsSelected

c# - 在派生类中通过 .NET 反射调用 protected 泛型方法