异步方法未等待 [英] The async method is not await

查看:85
本文介绍了异步方法未等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此线程中 ,我可以解决问题,使用事件在构造函数中等待异步方法.在这种情况下,该事件使用不带参数的委托.

In this thread, I could solve my problem to wait a async method in the constructor, using an event. In this case, the event uses a delegate without parameters.

但是现在我需要等待视图模型完成,以等待异步方法可以继续.

But now I need to wait the view model finishes to wait the async method to can continue.

我的主视图模型中有以下代码:

I have this code in my main view model:

public void printMethod()
{
    SecondViewModel mySeocViewModel = new SecondViewModel(myParameter);
    SecondView mySecondView = new SecondView();
    mySecondView.DataContext = mySeocViewModel;

    //I have to wait until it finished to can print the user control
    //The problem is that this point is reached before mySecondViewModel finish.
}

第二个视图模型中的代码:

The code in my second view model:

public docFacturasViewModel(MyType parameter)
{
    this.GetDataFromDatabaseEvent += OnGetDataFromDatabase;

    GetDataFromDatabaseEvent(parameter);
}


public delegate void GetDataFromDatabaseEventHandler(MyType parameter);
public event GetDataFromDatabaseEventHandler GetDataFromDataBaseEvent;

private async void OnBuscarDatos(MyType paramter)
{
    await getDataFromDatabaseAsync(parameter);

    //Fill the data of the properties of the view model with the data from database
}


private async Task getDataFromDatabaseAsync(MyType parameter)
{
    _myResult = (await getdataAsync(parameter)).FirstOrDefault();
}

如果我调试代码,它将以这种方式运行:

If i debug the code, it runs in this way:

  • 到达主视图模型中的行以创建第二个视图模型.
  • 在第二个视图模型中,它到达OnGetDataFromDatabase方法中的第一行,即等待行.
  • 主视图模型继续执行代码,因此它不会等待数据库结果.
  • 过一会儿,在第二个视图模型中,方法OnGetDataFromDatabase完成并在等待之后继续下一行代码.

我不明白为什么在第二个视图模型中代码不等到从数据库中获取数据,因为我使用的是await关键字,并且所有代码都与发布时相同我在开始时就链接了.

I don't understand why in the second view model the code doesn't wait until get the data from the database, because I am using the await keyword and all the code is the same than in the case of the post that I link at the beggining.

谢谢.

如果第二个视图模型的构造函数使用此代码:

If the constructor of the second view model I use this:

Task.Run(() => buscarDatosAsync(paramFacturaParaImprimir)).Wait();

然后,主视图模型要等到第二个视图模型完成才能从数据库中获取数据.

Then the main view model wait until the second view model finish to get the data from database.

但是当我在第一种情况下尝试此方法时,在我链接并在另一篇文章中询问的情况下,该方法没有等待,因此我不明白为什么在一种情况下我必须使用委托在另一种情况下,我可以使用任务并等待其完成.

But when I tried this method in the first case, in the case that I linked and I asked in the other post, the method is not waited, so I don't understand why in one case I have to use a delegate and in the other case I can use a task and wait it finishes.

推荐答案

async void方法到达await时,控件将返回给调用方.显然,当您需要操作结果来继续处理时,这不是最好的主意.除此之外,我不希望构造函数具有创建即发即弃任务的副作用.

When an async void method reaches an await, the control is returned to the caller. Obviously, this is not the best idea when you need the result of the operation to continue processing. Besides this, I wouldn't expect a constructor to have the side effect of creating fire-and-forget tasks.

如果您需要数据的结果作为对象构造的一部分,我建议您采用工厂方法:

If you need the result of the data as part of the construction of the object, I would suggest you a factory approach:

private SecondViewModel()
{
}

public static async Task<SecondViewModel> CreateAsync(MyType parameter)
{
    var result = new SecondViewModel();

    result.SomeData = await getDataFromDatabaseAsync(parameter);

    return result;
}

您将像这样使用它:

public async Task printMethod()
{
    SecondViewModel mySeocViewModel = await SecondViewModel.CreateAsync(myParameter);
    SecondView mySecondView = new SecondView();
    mySecondView.DataContext = mySeocViewModel;
}

这篇关于异步方法未等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆