private TaskCompletionSource<bool> response;
private string _text = "";
public void SetResult(bool result)
{
this.response.SetResult(result);
}
public async Task<bool> SendYesNo()
{
response = new TaskCompletionSource<bool>();
MessageBox.Show(this._text, "", MessageBoxButtons.YesNo);
this._text = "";
return response.Task.Result;
}
I'm using this code which is executed in a JavaScript script file so I can't call the await keyword.
I want to return a boolean after I set it using SetResult
. 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.
How to achieve this without Tasks (as I can't use the await keyword in JavaScript)?
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.