C# - Return a value asychronously

前端 未结 1 354
小蘑菇
小蘑菇 2021-01-28 07:42
    private TaskCompletionSource response;
    private string _text = \"\";

    public void SetResult(bool result)
    {
        this.response.SetResult(res         


        
相关标签:
1条回答
  • 2021-01-28 08:32

    If the response is not set, it will wait until it's set and will not return anything until it's set. It also has to be asychronous.

    Those two requirements are the exact opposite of each other.

    If you need to wait until the result is available, then you need it to be synchronous (that's the definition of synchronous). There are various hacks that can make this work - calling Result is one of them.

    If you need it to be asynchronous, then you can't wait until the result is available. In that case you should look into creating a JavaScript promise/deferred object and notify that object when the result arrives.

    0 讨论(0)
提交回复
热议问题