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
4
votes
4 answers

Should this code return a Task or Task?
I was reading The Nature of TaskCompletionSource, a post by Stephen Toub. public static Task RunAsync(Action action) { var tcs = new TaskCompletionSource(); ThreadPool.QueueUserWorkItem(_ => { try { …
afaolek
  • 8,452
  • 13
  • 45
  • 60
3
votes
1 answer

Avoiding allocations and maintaining concurrency when wrapping a callback-based API with an async API on a hot path

I have read a number of articles and questions here on StackOverflow about wrapping a callback based API with a Task based one using a TaskCompletionSource, and I'm trying to use that sort of technique when communicating with a Solace PubSub+…
allmhuran
  • 4,154
  • 1
  • 8
  • 27
3
votes
1 answer

ASP.NET MVC POST waiting indefinitely on first attempt

I have a standard form made with Html.BeginForm that posts to an async action in a controller. It looks like this (this is an outline, not the actual code): [HttpPost] public async Task Index(UserCreds creds) { try …
3
votes
3 answers

Async Tasks, Cancellation, and Exceptions

I'm currently learning how to properly expose asynchronous parts of our library API with Tasks so they can be easier and nicer to use for customers. I decided to go with the TaskCompletionSource approach of wrapping a Task around it that doesn't get…
Joey
  • 344,408
  • 85
  • 689
  • 683
3
votes
2 answers

Wrapping a library that uses the Event-Based Asyncronous Pattern, for use with Async/Await

I'm using the async/await pattern throughout my code. However, there's one API which used the event-based asyncronous pattern. I've read on MSDN, and several StackOverflow answers, that the way to do this is to use a TaskCompletionSource. My…
3
votes
1 answer

Awaiting a Task never completes even though its state transitions to 'RanToCompletion'

First, apologies -- I'm unable to reproduce this behaviour in a suitably simple sample application. This functionality was working before some refactoring of the calling code. I'm trying to use a TaskCompletionSource to signal the end of an async…
Ive
  • 457
  • 5
  • 19
2
votes
3 answers

Why will the synchronous continuations no longer be executed synchronously?

In the following article, Stephen Toub describes how to build a ManualResetEvent using the TPL. In particular, he states that in the following method: public void Set() { var tcs = m_tcs; Task.Factory.StartNew( s =>…
Marcin
  • 131
  • 6
2
votes
0 answers

Set TaskCompletionSource.Task status as running

I have an external long running process (web API call) which I have to track in order to know when it completes. I'd like to encapsulate it in a task. TaskCompletionSource seems to be the right tool to achieve that. But the task associated to…
Patrice
  • 29
  • 3
2
votes
1 answer

Awaiting a single .NET event with a ValueTask

I have a simple ITimer interface that has just a classic Elapsed .NET event that is raised after the a certain timespan. interface ITimer { void Start(TimeSpan interval); void Stop(); public event EventHandler Elapsed; } Now I would…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
2
votes
0 answers

TaskCompletionSource object becomes null in async method when code is compiled in release mode

I am using .Net version 4.6.1 and VS2015 Update 3. I am facing a weird issue as a piece of code is working fine when compiled in Debug mode, however, it fails with NullReferenceException when compiled in Release mode. I have an async method which…
prem
  • 3,348
  • 1
  • 25
  • 57
2
votes
1 answer

Cancelling async method that calls events

I have a headless UWP application that uses an external library to connect to a serial device and send some commands. It runs an infinite loop (while true) with a 10 minute pause between loops. The measurement process takes around 4 minutes. The…
2
votes
2 answers

How to substitute synchronization context / task scheduler to another one inside TaskCompletionSource.Task for ConfigureAwait(false)?

Assume I created a library containing such method: Task MyLibraryMethodAsync() { var taskCompletionSource = new TaskCompletionSource(); Action myWorkItem = () => { // Simulate some work. //…
2
votes
2 answers

Force any task to be attached to parent

I'm trying to create an extension method that will make any task attached to parent. Extension code: internal static class TaskHelpers { public static Task AttachToParrent(this Task task) { var tsc = new…
2
votes
1 answer

Unwrapping an async operation and it's async callback in a TaskCompletionSource

I have a method in a service that gets called by my view model to fetch an image. The image is fetched from an external library (iOS API in Xamarin) which uses a callback mechanic instead of being awaitable. In order to make my method awaitable, I…