Questions tagged [taskcompletionsource]

TaskCompletionSource produces a Task unbound to a delegate, providing access to the consumer side through the Task property.

TaskCompletionSource produces a Task unbound to a delegate, providing access to the consumer side through the Task property.

References

79 questions
255
votes
11 answers

When should TaskCompletionSource be used?

AFAIK, all it knows is that at some point, its SetResult or SetException method is being called to complete the Task exposed through its Task property. In other words, it acts as the producer for a Task and its completion. I saw here the…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
40
votes
3 answers

How to combine TaskCompletionSource and CancellationTokenSource?

I have such code (simplified here) which awaits finishing task: var task_completion_source = new TaskCompletionSource(); observable.Subscribe(b => { if (b) task_completion_source.SetResult(true); }); await…
35
votes
2 answers

Timeout an async method implemented with TaskCompletionSource

I have a blackbox object that exposes a method to kick of an async operation, and an event fires when the operation is complete. I have wrapped that into an Task BlackBoxOperationAysnc() method using TaskCompletionSource - that works…
Ricibob
  • 7,505
  • 5
  • 46
  • 65
23
votes
5 answers

Task FromResult vs TaskCompletionSource SetResult

What is the difference concerning the functionality and meaning of the TaskCompletionSource + SetResult vs Task + FromResult in the SendAsync method? protected override Task SendAsync(HttpRequestMessage request,…
19
votes
3 answers

TaskCompletionSource throws "An attempt was made to transition a task to a final state when it had already completed"

I want to use TaskCompletionSource to wrap MyService which is a simple service: public static Task ProcessAsync(MyService service, int parameter) { var tcs = new TaskCompletionSource(); //Every time ProccessAsync is called…
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
18
votes
1 answer

How to preserve await behavior with TaskCompletionSource.SetException?

(This is a new attempt at this question which now demonstrates the issue better.) Let's say we have a faulted task (var faultedTask = Task.Run(() => { throw new Exception("test"); });) and we await it. await will unpack the AggregateException and…
usr
  • 168,620
  • 35
  • 240
  • 369
13
votes
1 answer

What is the purpose of TaskCreationOptions with a TaskCompletionSource?

There's something unclear to me about the inner workings of TaskCompletionSource<>. When creating a simple Task<> using the Factory, I expect this task to be enqueued in a thread pool, unless I specify TaskCreationOptions.LongRunning, where it will…
uzul
  • 1,096
  • 9
  • 23
11
votes
2 answers

How to cancel a TaskCompletionSource using a timeout

I have the function that I call asynchronously using the await keyword: public Task RequestStateForEntity(EntityKey entity, string propName) { var tcs = new TaskCompletionSource(); try { var…
Retrocoder
  • 4,483
  • 11
  • 46
  • 72
9
votes
3 answers

Why does my TCS not await?

The async keyword do cause the CIL to change (even if there's no await inside the method), but it is primarily to allow await to be present. But I did not expect the following to happen: static void Main(string[] args) { Task t = Go(); …
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
6
votes
1 answer

Mocking Async Methods

We are writing unit tests for async code using MSTest and Moq. So we have some code that looks something like : var moq = new Mock(); moq.Setup(m => m.GetAsync()) .Returns(Task.FromResult(10)); Or like this on projects that have a more…
swestner
  • 1,881
  • 15
  • 19
6
votes
3 answers

Can one detect uncontrolled cancellation from .NET library code?

I've found that I can't distinguish controlled/cooperative from "uncontrolled" cancellation of Tasks/delegates without checking the source behind the specific Task or delegate. Specifically, I've always assumed that when catching an…
5
votes
1 answer

Do Tasks generated by TaskCompletionSource need to be Dispose()d?

I'm using TaskCompletionSource in my software to distribute network packets to async/await methods. So in my code, there are various points where the software needs to wait for a network packet getting demultiplexed from the receive handler and…
Scharle
  • 140
  • 1
  • 7
5
votes
2 answers

What's causing a deadlock?

I'm facing a deadlock-issue in a piece of code of mine. Thankfully, I've been able to reproduce the problem in the below example. Run as a normal .Net Core 2.0 Console application. class Class2 { static void Main(string[] args) { …
Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
4
votes
1 answer

Wrapping events with TaskCompletionSource vs. BufferBlock

There's a pattern discussed by Lucian here ( Tip 3: Wrap events up in Task-returning APIs and await them ). I am trying to implement it on a frequently called method that looks something like the contrived code below: public Task BlackBoxAsync() {…
4
votes
1 answer

Should I always complete a TaskCompletionSource?

What happens to a TaskCompletionSource and its Task if the TaskCompletionSource is never completed (i.e. SetCancelled, SetException or SetResultis never called? Would the Task live on forever because it never completes? In the below example I have a…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
1
2 3 4 5 6