1

I'm implementing some advanced scenario where I think I need TaskCompletionSource:

var tcs = new TaskCompletionSource<object>(null, TaskCreationOptions.LongRunning);

The code fails on my machine:

System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: creationOptions'

I tried targeting different .NET Framework versions without any luck. What am I doing wrong?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
UserControl
  • 14,766
  • 20
  • 100
  • 187
  • 1
    The error is clear - you used the wrong creation option. That option doesn't make sense anyway, a TCS isn't a running task so it can't be a *long* running task. It's a mechanism that allows code to signal completion of whatever the code was doing through a cold Task. – Panagiotis Kanavos Mar 08 '18 at 08:21
  • If you check the [TaskCompletionSource.cs](https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/TaskCompletionSource.cs,81) file you'll see that a) you don't need to pass a null state and b) the options are passed to a [Task constructor](https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/Task.cs,351) that only allows `AttachedToParent` and `.RunContinuationsAsynchronously` – Panagiotis Kanavos Mar 08 '18 at 08:27

0 Answers0