本文介绍了刷新DataServiceCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有代码可以刷新使用BeginExecute异步等待任务在WPF客户端中加载的DataServiceCollection,如下所示:

I wonder if there is a code there that refresh my DataServiceCollection that is loaded in a WPF client using BeginExecute async await Task as show below:

public static async Task<IEnumerable<TResult>> ExecuteAsync<TResult>(this DataServiceQuery<TResult> query)
{
    //Thread.Sleep(10000);
    var queryTask = Task.Factory.FromAsync<IEnumerable<TResult>>(query.BeginExecute(null, null), (asResult) =>
    {
        var result = query.EndExecute(asResult).ToList();
        return result;
    });
    return await queryTask;
}

我将扩展方法称为:

public async void LoadData()
{
   _ctx = new TheContext(the URI);
   _dataSource = new DataServiceCollection<TheEntity>(_ctx);
   var query = _ctx.TheEntity;
   var data = await Task.Run(async () => await query.ExecuteAsync());
   _dataSource.Clear(true);
   _dataSource.Load(data);
}
  1. 在ViewModel中调用LoadData
  2. 使用SQL Management Studio手动更改字段值
  3. 再次调用LoadData <<<<不刷新!!!

同时,如果我使用Load方法,则刷新数据时不会出现任何问题,如下所示:

Meanwhile, If I use Load method the data is refreshed without any problems as:

var query = _ctx.TheEntity;
_dataSource.Load(query);

另一个问题是我不知道如何取消客户端更改.最后一个问题是MergeOption是否对BeginExecute..EndExecute有任何影响,或者仅适用于Load方法?

Another issue is that I do not know how to Cancel client changes. Last question is whether the MergeOption has any effect to BeginExecute..EndExecute or it only works with Load method?

推荐答案

我怀疑数据上下文不喜欢从多个线程访问.

I suspect that the data context does not like being accessed from multiple threads.

我建议您首先从扩展方法中删除所有处理:

I recommend that you first remove all processing from your extension method:

public static Task<IEnumerable<TResult>> ExecuteAsync<TResult>(this DataServiceQuery<TResult> query)
{
  return Task.Factory.FromAsync(query.BeginExecute, query.EndExecute, null);
}

您应使用 without 跳转到后台线程:

Which you should use without jumping onto a background thread:

public async Task LoadDataAsync()
{
  _ctx = new TheContext(the URI);
  _dataSource = new DataServiceCollection<TheEntity>(_ctx);
  var query = _ctx.TheEntity;
  var data = await query.ExecuteAsync();
  _dataSource.Clear(true);
  _dataSource.Load(data);
}

这篇关于刷新DataServiceCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:34