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
2
votes
1 answer

TaskCompletionSource usage

What would I receive in result variable if completion source was cancelled? async void SomeMethod() { ..... Run(); var result = await GetResult(); ..... } Task GetResult() { return…
Access Denied
  • 8,723
  • 4
  • 42
  • 72
2
votes
1 answer

Replacing TaskCompletionSource with Observable

In my .NET 4.0 library I have a piece of code that sends data over the network and waits for a response. In order to not block the calling code the method returns a Task that completes when the response is received so that the code can call the…
Petrik
  • 1,961
  • 13
  • 23
1
vote
1 answer

JSON-RPC - Handling exceptions with TaskCompletionSource (SetException)

There is a JSON-RPC API, which I'm currently implementing. It can be tested here. The problem is that if an incorrect DTO model is passed to SendAsync, JsonSerializer.Deserialize is going to throw a JsonException, which is not handled by…
nop
  • 4,711
  • 6
  • 32
  • 93
1
vote
1 answer

Why is the task from TaskCompletionSource not completed after 5 seconds?

When I set the CancellationTokenSource to cancel after 5 seconds. The TaskCompletionSource will not be cancelled. [Test] public async Task Test() { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); var completionSource = new…
Rene
  • 526
  • 6
  • 18
1
vote
1 answer

TaskCompletionSource and not thread-safe library

I have C# avalonia application uses some not thread-safe library via SDK provided by the developer. More specifically is Windows Zoom SDK. Some SDK functions are built on the event driven pattern. After calling SDK methods, application must wait for…
1
vote
1 answer

How to use async operation in IO-Bound with TaskCompletionSource?

I want to implement an asynchronous mechanism in IO-Bound, and how can I implement this with TaskCompletionSource without consuming a new thread? This following sample to create new thread in thread pool, but I am looking for a new approach by…
1
vote
2 answers

How to best use TaskCompletionSource with SemaphoreSlim

This is an evolution of my previous question. To recap, I have 3rd party WebSocket that I need to consume where the request is sent in one method and the response is given in another. I'm attempting to convert this to a Task. I read this MSDN…
1
vote
1 answer

When are .NET TaskCompletionSource Tasks eligible for GC (not rooted)?

I'm writing a utility class for some asynchronous code, and I want to ensure that I don't create memory leaks with the design. Suppose I've got code which executes similarly to the class below. (Obviously you wouldn't write code that works like…
1
vote
2 answers

Build an IAsyncEnumerable using TaskCompletionSource

I have a method that accepts an IEnumerable and returns it transformed using the yield operator. To transform one element of the enumerable, I need to first know the value of another element. Therefore, I thought of using a TaskCompletionSource to…
1
vote
2 answers

Using TaskCompletionSource Within An await Task.Run Call

I am getting unexpected behavior that I would like to shed some light on. I've created a simple example to demonstrate the problem. I call an async function using Task.Run, which will continuously generate results, and uses IProgress to deliver…
Cloudless
  • 19
  • 3
1
vote
2 answers

Using Polly in C#, can I wait until a timespan elapses OR a task terminates before retrying?

I am trying to use the Polly package in C#. I want to run some code and then, if it fails, wait and retry. Currently my loop looks similar to this: var successful = false while (!successful){ // Try to perform operation. successful =…
Claus Appel
  • 379
  • 1
  • 4
  • 13
1
vote
1 answer

How do I unit test a class that depends on TaskCompletionSource?

I have a class that depends on TaskCompletionSource An example of the class looks like this: public ExampleClass { private TaskCompletionSource _tcs; public async Task GetFooAsync() { _tcs = new…
Fred
  • 12,086
  • 7
  • 60
  • 83
1
vote
5 answers

Return with await when wrapping old async pattern into TaskCompletionSource?

I am studying C# Asnc-await pattern and currently reading Concurrency in C# Cookbook from S. Cleary He discusses wrapping old non TAP async patterns with TaskCompletionSource (TCS) into TAP constructs. What I dont get is, why he just returns the…
J4ni
  • 217
  • 1
  • 2
  • 12
1
vote
1 answer

TaskCompletionSource performs slowly

By looping through multiple instances of TaskCompletionSource in the synchronous method, more than 5 programs will become slow to execute, but in the asynchronous method you don't have this problem. This is problematic code Using the synchronization…
汪泽林
  • 11
  • 2
1
vote
2 answers

Can I guarantee runnable task continuations have been run?

Here's a significantly reduced test case from a piece of code I'm working on: var i = 0; var taskCompletionSource = new TaskCompletionSource(); var task = taskCompletionSource.Task; Task.Run(async () => { await task; i =…