Jordan
Jordan

Reputation: 9901

What if I never respond to an await using a TaskCompletionSource?

I'm using a TaskCompletionSource to create a kind of asynchronous user interface transaction.

var tcs = new TaskCompletionSource<IAutoQuestionAnswer>();

a_question.AnswerSelected += (s, e) => tcs.TrySetResult(e.Item);

_navigationService.Navigate(ViewNames.AutoQuestionView, a_question);

return tcs.Task;

If AnswerSelected is never raised and TrySetResult is never called on the TCS, will this cause a problem? I have a home button on the AutoQuestionView UI that doesn't answer the question at all but takes the user back to the start of the application. In this case, TrySetResult will never be called. I've already checked whether threads build up and they don't. I am completely fine if the code following this is simply never called but I want to be sure it won't be something that bites me later on.

Upvotes: 1

Views: 330

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456397

You should not do this. The framework assumes that tasks will complete.

As @SLaks noted, if you don't, then you will cause memory leaks.

Upvotes: 3

Related Questions