I have a long running process in a WCF service that may return a large XML. To avoid running out of memory, I decided it would be a good idea to query the xml in chunks. so I did this:
Task<IEnumerable<XElement>> aTask = Task.Factory.StartNew(() => this.QueryServer());
And the implementation of QueryServer()
private IEnumerable<XElement> QueryServer()
{
using (var s = new MyService())
{
for (DateTime halfADay = StartDate; halfADay < EndDate.Date.AddHours(12); halfADay = halfADay.AddHours(12))
{
yield return s.MyServiceClient.DownloadLargeXElement(
halfADay,
halfADay.AddHours(12));
}
}
}
My problem, is that the UI will hang if I use TaskScheduler.FromCurrentSynchronizationContext() but If I don't use it then my UI won't be updated once it's done querying the XML
aTask.ContinueWith(
(a) =>
{
if(a.IsCompleted)
{
foreach(XElement row in a.Result.Select(x => x.Elements()).First())
{
DataRow dataRow = this.XElementToDataRow(row);
MyTable.Rows.Add(row);
}
}
},
**TaskScheduler.FromCurrentSynchronizationContext()**);
I know this is happening because when it reaches my foreach it then queries the server but at this moment we're back on the UI thread. Is there a better way to go about doing this?
No comments:
Post a Comment