Last active
September 4, 2020 13:08
-
-
Save ssomers/17c961ff80b17955fd8674d191d2c149 to your computer and use it in GitHub Desktop.
Task.Run differs from Task.Factory.StartNew with TaskCreationOptions.DenyChildAttach & TaskScheduler.Default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Threading.Tasks.Dataflow; | |
internal static class Program | |
{ | |
private static void Main() | |
{ | |
var chan = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 2 }); | |
Task.Factory.StartNew(async () => | |
{ | |
for (int i = 1; i <= 4; ++i) | |
{ | |
while (!chan.Post(i)) await Task.Delay(10); | |
Console.WriteLine($"produced {i}"); | |
} | |
chan.Complete(); | |
Console.WriteLine("stop producing"); | |
}, | |
CancellationToken.None, | |
TaskCreationOptions.DenyChildAttach, | |
TaskScheduler.Default); | |
var task = Task.Factory.StartNew(async () => | |
{ | |
try | |
{ | |
while (true) | |
{ | |
int i = await chan.ReceiveAsync(); | |
Console.WriteLine($"consumed {i}"); | |
} | |
} | |
catch (InvalidOperationException) | |
{ | |
Console.WriteLine("stop consuming"); | |
} | |
}, | |
CancellationToken.None, | |
TaskCreationOptions.DenyChildAttach, | |
TaskScheduler.Default); | |
task.Wait(); | |
Console.WriteLine("stop waiting"); | |
Thread.Sleep(2020); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When run as a console app, this output for instance:
suggesting that the 2nd task continues after it has ended.
Replace
Task.Factory.StartNew
withTask.Run
and delete the 3 extra arguments, and the output starts making sense.Adding a
.Unwrap()
after theStartNew(...)
instead gives the same reasonable output.