gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:19:32 24 4
gpt4 key购买 nike

我正在将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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com