2 years ago

#72746

test-img

zhusp

Why TransformBlock get different results?

Why TransformBlock get different results each time it executes?

Expect:

  • 1,2,3
  • 4,5,6,7,8,9

Result:

  • 4,2,3
  • 1,5,6,7,8,9
  • 1,2,3
  • 4,5,6,7,8,9
  • 4,2,3
  • 1,5,6,7,8,9
  • 1,2,3
  • 4,5,6,7,8,9
  • 1,2,3
  • 4,5,6,7,8,9
  • 1,5,3
  • 4,2,6,7,8,9
  • 1,2,3
  • 4,5,6,7,8,9
  • 5,2,8
  • 1,4,6,7,3,9
public static TransformBlock<int, string>  tbInt = new TransformBlock<int, string>((i) =>
{
    return i.ToString();
});
static void Main(string[] args)
{

    while (true)
    {
        Task t1 = Task.Run(() => {
            var res = TransformInt(new int[] { 1, 2, 3 });
            Console.WriteLine(string.Join(',', res));
        });
        Task t2 = Task.Run(() =>
        {
            var res = TransformInt(new int[] { 4, 5, 6, 7, 8, 9 });
            Console.WriteLine(string.Join(',', res));
        });
        Task.WaitAll(new Task[] { t1, t2 });
        System.Threading.Thread.Sleep(1000);
    }
}
static IEnumerable<string> TransformInt(int[] sources)
{
    foreach (var item in sources)
    {
        tbInt.Post(item);
        yield return tbInt.Receive();
    }
}

How can I get expected?

c#

.net-core

dataflow

tpl-dataflow

0 Answers

Your Answer

Accepted video resources