且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#异步方法并返回等待

更新时间:2022-05-10 21:19:50

如果您这样调用此方法:

If you're calling this method like this:

await FindAsync(); // this method waits for the task to complete

然后在此方法中返回await毫无意义,您可以将其更改为:

Then it doesn't make any sense to return await inside this method, you can just change it to:

public Task<T> FindAsync(params object[] keys)
{
    return this.context.FindAsync(keys); // Start and return the task
}

然后,调用方等待任务完成.

Then the caller awaits the task to Complete.