Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何等待运行异步方法的任务_C#_Wpf_Entity Framework_Nested_Async Await - Fatal编程技术网

C# 如何等待运行异步方法的任务

C# 如何等待运行异步方法的任务,c#,wpf,entity-framework,nested,async-await,C#,Wpf,Entity Framework,Nested,Async Await,我正在编写WPF应用程序,最近开始使用wait/async,这样GUI线程就不会执行任何耗时的操作 我的问题是我想使用实体框架从db异步加载两个集合。我知道我不能在DbContext上调用两个ToListAsync()方法,所以我想使用tasks 我编写了异步方法LoadData(),它应该等待完成LoadNotifications(),然后调用LoadCustomers() 但是当执行到时,等待这个.context.MailingDeliveryNotifications.toListSync

我正在编写WPF应用程序,最近开始使用wait/async,这样GUI线程就不会执行任何耗时的操作

我的问题是我想使用实体框架从db异步加载两个集合。我知道我不能在DbContext上调用两个
ToListAsync()
方法,所以我想使用tasks

我编写了异步方法LoadData(),它应该等待完成
LoadNotifications()
,然后调用
LoadCustomers()

但是当执行到
时,等待这个.context.MailingDeliveryNotifications.toListSync()
它创建了另一个任务,但不知何故它并不关心我的
LoadData()
方法中的
task.Wait()
,因此它在完成对DbContext的第一次调用之前调用
LoadCustomers()

守则:

    public async void LoadData()
    {
        Task task = this.LoadNotifications();
        task.Wait();
        await this.LoadCustomers();
    }

    private Task LoadNotifications()
    {
        return Task.Run(() => this.LoadNotificationsAsync());
    }

    private async void LoadNotificationsAsync()
    {
        List<MailingDeliveryNotification> res = await this.context.MailingDeliveryNotifications.ToListAsync();
        this.Notifications = new ObservableCollection<MailingDeliveryNotification>(res);
    }

    private Task LoadCustomers()
    {
        return Task.Run(() => this.LoadNotificationsAsync());
    }

    private async void LoadCustomersAsync()
    {
        List<Customer> res = await this.context.Customers.ToListAsync();
        this.Customers = new ObservableCollection<Customer>(res);
    }
public异步void LoadData()
{
Task Task=this.LoadNotifications();
task.Wait();
等待此消息。LoadCustomers();
}
私有任务加载通知()
{
返回Task.Run(()=>this.LoadNotificationsAsync());
}
私有异步void LoadNotificationsAsync()
{
List res=wait this.context.MailingDeliveryNotifications.ToListSync();
this.Notifications=新的ObservableCollection(res);
}
私有任务LoadCustomers()
{
返回Task.Run(()=>this.LoadNotificationsAsync());
}
私有异步void加载程序CustomerAsync()
{
List res=wait this.context.Customers.toListSync();
这个。客户=新的可观察收集(res);
}
我知道我可以用这个代码解决这个问题

    public async void LoadData()
    {
        List<MailingDeliveryNotification> res = await this.context.MailingDeliveryNotifications.ToListAsync();
        this.Notifications = new ObservableCollection<MailingDeliveryNotification>(res);

        List<Customer> res2 = await this.context.Customers.ToListAsync();
        this.Customers = new ObservableCollection<Customer>(res2);
    }
public异步void LoadData()
{
List res=wait this.context.MailingDeliveryNotifications.ToListSync();
this.Notifications=新的ObservableCollection(res);
List res2=wait this.context.Customers.toListSync();
this.Customers=新的可观察集合(res2);
}
但是,当我需要添加另一个集合来从db加载时,这种方法将增长很多。我想保持代码整洁。

简化代码:

public async Task LoadDataAsync()
{
    await LoadNotificationsAsync();
    await LoadCustomersAsync();
}

private async Task LoadNotificationsAsync()
{
    var res = await context.MailingDeliveryNotifications.ToListAsync();
    Notifications = new ObservableCollection<MailingDeliveryNotification>(res);
}

private async Task LoadCustomersAsync()
{
    var res = await context.Customers.ToListAsync();
    Customers = new ObservableCollection<Customer>(res);
}
公共异步任务LoadDataAsync()
{
等待LoadNotificationsAsync();
等待LoadCustomerAsync();
}
专用异步任务LoadNotificationsAsync()
{
var res=await context.MailingDeliveryNotifications.toListSync();
通知=新的可观察收集(res);
}
专用异步任务加载程序CustomerAsync()
{
var res=await context.Customers.ToListAsync();
客户=新的可观察收集(res);
}
或者可能只是:

public async Task LoadDataAsync()
{
    Notifications = new ObservableCollection<MailingDeliveryNotification>(
        await context.MailingDeliveryNotifications.ToListAsync());

    Customers = new ObservableCollection<Customer>(
        await context.Customers.ToListAsync());
}
公共异步任务LoadDataAsync()
{
通知=新的ObservableCollection(
wait context.MailingDeliveryNotifications.toListSync());
客户=新的可观察收集(
wait context.Customers.ToListAsync());
}
简化您的代码:

public async Task LoadDataAsync()
{
    await LoadNotificationsAsync();
    await LoadCustomersAsync();
}

private async Task LoadNotificationsAsync()
{
    var res = await context.MailingDeliveryNotifications.ToListAsync();
    Notifications = new ObservableCollection<MailingDeliveryNotification>(res);
}

private async Task LoadCustomersAsync()
{
    var res = await context.Customers.ToListAsync();
    Customers = new ObservableCollection<Customer>(res);
}
公共异步任务LoadDataAsync()
{
等待LoadNotificationsAsync();
等待LoadCustomerAsync();
}
专用异步任务LoadNotificationsAsync()
{
var res=await context.MailingDeliveryNotifications.toListSync();
通知=新的可观察收集(res);
}
专用异步任务加载程序CustomerAsync()
{
var res=await context.Customers.ToListAsync();
客户=新的可观察收集(res);
}
或者可能只是:

public async Task LoadDataAsync()
{
    Notifications = new ObservableCollection<MailingDeliveryNotification>(
        await context.MailingDeliveryNotifications.ToListAsync());

    Customers = new ObservableCollection<Customer>(
        await context.Customers.ToListAsync());
}
公共异步任务LoadDataAsync()
{
通知=新的ObservableCollection(
wait context.MailingDeliveryNotifications.toListSync());
客户=新的可观察收集(
wait context.Customers.ToListAsync());
}

另外,
async void
方法很顽皮。不要使用
Task。运行
只是为了调用异步方法。不需要它,该方法已经返回了一个任务,并且已经在后台运行。除了事件处理程序外,不要使用
async void
。你不能等他们,就这样。无论是
async void LoadNotificationsAsync()
还是
async void LoadData
都不能等待。另外,
async void
方法也很糟糕。不要使用
任务。运行
只是为了调用异步方法。不需要它,该方法已经返回了一个任务,并且已经在后台运行。除了事件处理程序外,不要使用
async void
。你不能等他们,就这样。无论是
async void LoadNotificationsAsync()
还是
async void LoadData
都不能等待。非常有效,谢谢。但是它是如何工作的呢?LoadNotifications重新运行任务时,它是返回执行整个方法的任务,还是返回来自
context.MailDeliveryNotifications.toListSync()
call的任务?非常有效,谢谢。但是它是如何工作的呢?LoadNotifications重新运行任务时,它是返回执行整个方法的任务,还是返回来自
context.MailDeliveryNotifications.toListSync()调用的任务?