TPL DataFlow join block

NALB 61 Reputation points
2023-04-04T09:42:26.22+00:00

Hello , I have a A workflow of blocks and links part of it is the below diagram I need to collect data from both Test1Block and Test2Block which they return type of object Test1 and Test2 then they enter join block the output is passed to another FinalBlock ... Actually I do not need to join the data in the join block as they are in different types , both Test1Block and Test2Block output text files so i really do not want to use join block but the only way to wait for them to output the text files are to do join and not break the workflow I have ... I have tried the below code however I never hit the join block or after them .. All the examples of join block they use post to send to join block however my current situation that the data comes from previous blocks data.

can you tell me how can i achieve the idea I am trying ? and what is my problem ?

 
 var Test1Block = new TransformBlock<x, Test1>(async item =>
            {   
                return TextFile1;
            });


 var Test2Block = new TransformBlock<x, Test2>(async item =>
            {   
                return TextFile2;
            });


var broadcast = new BroadcastBlock<Tuple<Test1,Test2>>(i => i);
var jonblock = new JoinBlock<Test1, Test2>();


 var FinalBlock = new TransformBlock<Tuple<Test1 , Test2>, XData>(async item =>
            {
                Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

                for (int i = 0; i < 2; i++)
                {
                    var data = JoinBlock.Receive();
                    Console.WriteLine("Dataaa of joinnnn *****************  ::: ", data);
                    Console.WriteLine("{0} + {1} ", data.Item1, data.Item2 );
                }
                
                var TheatIntelligenceGatheringFile = await TheatIntelligenceGathering.GetThreatIntelligenceGatheringFile();
                var TheatIntelligenceGatheringData = await TheatIntelligenceGathering.ExtractSuspiciousHostnames(TheatIntelligenceGatheringFile);
                return TheatIntelligenceGatheringData;

            });

Thank you in advance ..

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,375 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 36,391 Reputation points Microsoft Vendor
    2023-04-05T09:43:34.14+00:00

    Hi @NALB, welcome to Microsoft Q&A. It sounds like you don't want to use JoinBlock. Then you probably need to wait for Test1Block and Test2Block to complete before passing their results to FinalBlock. You may need to create a TaskCompletionSource for each test block, and Wait for the results to complete before passing them to the FinalBlock. After the testblock is completed, pass the result to FinalBlock.

    The following code is for reference only:

    var Test1CompletionSource = new TaskCompletionSource<Test1>();
    Test1Block.LinkTo(
        new ActionBlock<Test1>(test1 =>
        {
            Test1CompletionSource.SetResult(test1);
        })
    );
    await Task.WhenAll(Test1CompletionSource.Task, Test2CompletionSource.Task);
    var data = Tuple.Create(Test1CompletionSource.Task.Result, Test2CompletionSource.Task.Result);
    Console.WriteLine("{0} + {1} ", data.Item1, data.Item2 );
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments