当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Await an async function from setter property

问题描述:

I need to await to an async function from a property setter method.

public String testFunc()

{

get

{

}

set

{

//Await Call to the async func <asyncFunc()>

}

}

I understand we should not make async properties, so what is the optimal way to do this.

网友答案:

You can't make async properties and you shouldn't want to - properties imply fast, non blocking operations. If you need to perform a long running activity, as implied by you're wanting to kick of an async operation and wait for it, don't make it a property at all.

Remove the setter and make a method instead.

网友答案:

Use

public bool SomeMethod
{
  get { /* some code */ }
  set
  {
    AsyncMethod().Wait();
  }
}
public async Task AsyncMethod() {}

[EDIT]

分享给朋友:
您可能感兴趣的文章:
随机阅读: