Get result from async method

后端 未结 2 1126
礼貌的吻别
礼貌的吻别 2020-12-10 13:03

I have this method in my service:

public virtual async Task FindByIdAsync(string userId)
{
    this.ThrowIfDisposed();
    if (userId == null)
           


        
2条回答
  •  -上瘾入骨i
    2020-12-10 13:30

    • Using this in async methods without special thread-locked object is dangerous
    • If you cannot use await, use a code like following.

      Task task = TaskFindByIdAsync();
      
      task.Wait(); //Blocks thread and waits until task is completed
      
      User resultUser = task.Result;
      

提交回复
热议问题