[英]In a JoinBlock, receive a Target when the other Target is filled
I'm connecting a JoinBlock to a WriteOnceBlock and a BufferBlock to fill Targets 1 and 2. My goal is that every time the JoinBlock receives a message from the BufferBlock , it also requests the value that the WriteOnceBlock is holding.我将 JoinBlock 连接到 WriteOnceBlock 和 BufferBlock 以填充目标 1 和 2。我的目标是每次 JoinBlock 收到来自 BufferBlock 的消息时,它还请求 WriteOnceBlock 持有的值。
My first guess was that I could add a ContinueWith delegate to the Target2 Completion event, but that's not quite right - I need to attach to something like a Filled event that doesn't seem to exist.我的第一个猜测是我可以将 ContinueWith 委托添加到 Target2 Completion 事件,但这不太正确 - 我需要附加到似乎不存在的 Filled 事件之类的东西。
I also tried using the join block in non-greedy mode as a last-ditch effort, but that didn't change the output.我还尝试在非贪婪模式下使用 join 块作为最后的努力,但这并没有改变输出。
Am I missing something obvious here?我在这里遗漏了一些明显的东西吗?
Example:例子:
static void Main(string[] args)
{
var writeOnceBlockTest = new WriteOnceBlock<int>(i => i);
var queueBlockTest = new BufferBlock<string>();
var joinBlockTest = new JoinBlock<int, string>();
var actionBlockTest = new ActionBlock<System.Tuple<int, string>>(tuple => Console.WriteLine($"I received int {tuple.Item1} and string {tuple.Item2}."));
writeOnceBlockTest.LinkTo(joinBlockTest.Target1);
queueBlockTest.LinkTo(joinBlockTest.Target2, new DataflowLinkOptions{PropagateCompletion = true});
joinBlockTest.LinkTo(actionBlockTest, new DataflowLinkOptions { PropagateCompletion = true });
writeOnceBlockTest.Post(3);
queueBlockTest.Post("String1");
queueBlockTest.Post("String2");
writeOnceBlockTest.Post(4);
writeOnceBlockTest.Post(5);
queueBlockTest.Post("String3");
queueBlockTest.Post("String4");
queueBlockTest.Complete();
Console.ReadLine();
}
Desired output:期望的输出:
I received int 3 and string String1.
我收到了 int 3 和字符串 String1。
I received int 3 and string String2.
我收到了 int 3 和字符串 String2。
I received int 3 and string String3.
我收到了 int 3 和字符串 String3。
I received int 3 and string String4.
我收到了 int 3 和字符串 String4。
Actual output:实际输出:
I received int 3 and string String1.
我收到了 int 3 和字符串 String1。
The JoinBlock
isn't the right choice here although it does seem like the perfect fit. JoinBlock
在这里不是正确的选择,尽管它看起来很合适。 The WriteOnceBlock
, as you've found, only offers it's value once.正如您所发现的,
WriteOnceBlock
只提供一次它的价值。 However, you can read that value many times.但是,您可以多次读取该值。 With this you can use a
TransformBlock
to get the behavior your looking for.有了这个,您可以使用
TransformBlock
来获得您想要的行为。
public class JoinFlow
{
[Test]
public async Task TestWriteOnceBlock()
{
var writeOnceBlockTest = new WriteOnceBlock<int>(i => i);
var queueBlockTest = new BufferBlock<string>();
var transformBlockTest = new TransformBlock<string, Tuple<int, string>>(async str => Tuple.Create(await writeOnceBlockTest.ReceiveAsync(), str));
var actionBlockTest = new ActionBlock<Tuple<int, string>>(tuple => Console.WriteLine($"I received int {tuple.Item1} and string {tuple.Item2}."));
queueBlockTest.LinkTo(transformBlockTest, new DataflowLinkOptions { PropagateCompletion = true });
transformBlockTest.LinkTo(actionBlockTest, new DataflowLinkOptions { PropagateCompletion = true });
writeOnceBlockTest.Post(3);
queueBlockTest.Post("String1");
queueBlockTest.Post("String2");
writeOnceBlockTest.Post(4);
writeOnceBlockTest.Post(5);
queueBlockTest.Post("String3");
queueBlockTest.Post("String4");
queueBlockTest.Complete();
await actionBlockTest.Completion;
}
}
Outputs:输出:
I received int 3 and string String1.
I received int 3 and string String2.
I received int 3 and string String3.
I received int 3 and string String4.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.