Source Executor Block
-
Saturday, November 19, 2011 4:54 AM
Have you guys thought about adding a Source Executor Block? It would be nice to be able to kick off a data flow with a block that only produces messages.
Let me know if I am missing something.
Thanks,
Dave
http://me.davebettin.com- Changed Type Stephen Toub - MSFTMicrosoft Employee, Owner Saturday, November 19, 2011 8:49 PM
All Replies
-
Saturday, November 19, 2011 8:49 PMOwner
Hi Dave-
Thanks for the suggestion. We have, but that wouldn't be much different than just using a BufferBlock<T>. For example, let's say you wanted to implement a block which produced a message every N milliseconds... you could do that with code like:
public static ISourceBlock<T> CreateTimerBlock<T>(int interval, Func<T> generator)
{
var bb = new BufferBlock<T>();
var timer = new Timer(delegate { bb.Post(generator()); }, null, interval, inteval);
bb.Completion.ContinueWith(delegate { timer.Dispose(); });
return bb;
}More generally, here's a function that would supply you with delegates to post messages, signal completion, or signal errors:
public static ISourceBlock<T> CreateSourceBlock<T>(
Action<Action<T>,Action,Action<Exception>> executor)
{
IPropagatorBlock<T,T> bb = new BufferBlock<T>();
executor(t => bb.Post(t), () => bb.Complete(), e => bb.Fault(e));
return bb;
}With that function, I could do things like:
ISourceBlock<int> source = CreateSourceBlock<int>(async (post, complete, fault) =>
{
try
{
for(int i=0; i<10; i++)
{
await Task.Delay(1000);
post(i);
}
complete();
}
catch(Exception e) { fault(e); }
});I hope that helps.
- Proposed As Answer by Stephen Toub - MSFTMicrosoft Employee, Owner Saturday, November 19, 2011 8:49 PM
- Marked As Answer by Dave Bettin Monday, November 21, 2011 11:04 PM

