What would I receive in result variable if completion source was cancelled?
async void SomeMethod()
{
.....
Run();
var result = await GetResult();
.....
}
Task<SomeResult> GetResult()
{
return myCompletionSource.Task;
}
TaskCompletionSource myCompletionSource;
void Run()
{
myCompletionSource= new TaskCompletionSource();
TriggerSomeLongLastingLogicWhichWillCallCallBackBelow();
}
void SomeCallback()
{
if (someCondition)
{
myCompletionSource.SetResult(<someResult>);
}
else
{
myCompletionSource.SetCancelled();
}
}
I'm not quite sure whether this approach is correct.
- In other words is it a good practice to rely on task status rather than creating a wrapper for "someresult" with status variable?
- How to handle cancelled task? I'm not a fan of callbacks and don't like solution with ContinueWith, where I can analize task status.