本文整理汇总了C#中TransformBlock类的典型用法代码示例。如果您正苦于以下问题:C# TransformBlock类的具体用法?C# TransformBlock怎么用?C# TransformBlock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransformBlock类属于命名空间,在下文中一共展示了TransformBlock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MatchPipeline
public MatchPipeline(RiotApi riotApi, RiotQuerySettings querySettings, IMatchDetailProcessor matchDetailProcessor)
{
api = riotApi;
this.querySettings = querySettings;
this.matchDetailProcessor = matchDetailProcessor;
queryQueues.Add(querySettings.Queue);
// Create match producer
if (!querySettings.NoDownload)
{
playerMatchProducer = new PlayerMatchProducer(api, querySettings, queryQueues, testSynchronizer);
// Create blocks
ConsumeMatchBlock = new TransformBlock<MatchSummary, MatchDetail>(
async match => await ConsumeMatch(match),
new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 5 });
}
ConsumeMatchDetailBlock = new ActionBlock<MatchDetail>(
async match => await matchDetailProcessor.ConsumeMatchDetail(match),
new ExecutionDataflowBlockOptions() {
MaxDegreeOfParallelism = matchDetailProcessor.MaxDegreeOfParallelism
});
// Link blocks
if (!querySettings.NoDownload)
{
playerMatchProducer.MatchProducerBlock.LinkTo(ConsumeMatchBlock, new DataflowLinkOptions() { PropagateCompletion = false });
ConsumeMatchBlock.LinkTo(ConsumeMatchDetailBlock, new DataflowLinkOptions() { PropagateCompletion = false }, match => match != null);
ConsumeMatchBlock.LinkTo(DataflowBlock.NullTarget<MatchDetail>(), new DataflowLinkOptions() { PropagateCompletion = false });
}
}
示例2: TestTransformBlockConstructor
public void TestTransformBlockConstructor()
{
// SYNC
{
// without option
var block = new TransformBlock<int, string>(i => i.ToString());
Assert.False(block.InputCount != 0, "Constructor failed! InputCount returned a non zero value for a brand new TransformBlock.");
//with not cancelled token and default scheduler
block = new TransformBlock<int, string>(i => i.ToString(), new ExecutionDataflowBlockOptions { MaxMessagesPerTask = 1 });
Assert.False(block.InputCount != 0, "Constructor failed! InputCount returned a non zero value for a brand new TransformBlock.");
//with a cancelled token and default scheduler
var token = new CancellationToken(true);
block = new TransformBlock<int, string>(i => i.ToString(), new ExecutionDataflowBlockOptions { MaxMessagesPerTask = 1, CancellationToken = token });
Assert.False(block.InputCount != 0, "Constructor failed! InputCount returned a non zero value for a brand new TransformBlock.");
}
// ASYNC (a copy of the sync but with constructors returning Task<T> instead of T
{
// without option
var block = new TransformBlock<int, string>(i => Task.Factory.StartNew(() => i.ToString()));
Assert.False(block.InputCount != 0, "Constructor failed! InputCount returned a non zero value for a brand new TransformBlock.");
//with not cancelled token and default scheduler
block = new TransformBlock<int, string>(i => Task.Factory.StartNew(() => i.ToString()), new ExecutionDataflowBlockOptions { MaxMessagesPerTask = 1 });
Assert.False(block.InputCount != 0, "Constructor failed! InputCount returned a non zero value for a brand new TransformBlock.");
//with a cancelled token and default scheduler
var token = new CancellationToken(true);
block = new TransformBlock<int, string>(i => Task.Factory.StartNew(() => i.ToString()), new ExecutionDataflowBlockOptions { MaxMessagesPerTask = 1, CancellationToken = token });
Assert.False(block.InputCount != 0, "Constructor failed! InputCount returned a non zero value for a brand new TransformBlock.");
}
}
示例3: Run
public void Run()
{
Console.WriteLine("Generating first {0} powers of 2.", MaxItems);
var bufferBlock = new BufferBlock<int>();
Enumerable.Range(1, MaxItems)
.ToList()
.ForEach(i => bufferBlock.Post(i));
Console.WriteLine("Signaling completion to the source block.");
bufferBlock.Complete();
Console.WriteLine("Done.");
Console.WriteLine("Creating and linking the remaing blocks to the network.");
var transformBlock = new TransformBlock<int, double>(i =>
{
Thread.Sleep(200);
return Math.Pow(2, i);
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 1 });
var actionBlock = new ActionBlock<double>(async i =>
{
await Task.Delay(500);
Console.WriteLine(i);
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 10 });
bufferBlock.LinkTo(transformBlock, new DataflowLinkOptions { PropagateCompletion = true });
transformBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true });
Console.WriteLine("Waiting for the completion to be propagated through the network...");
actionBlock.Completion.ContinueWith(t =>
{
Console.WriteLine("Finished processing.");
Console.WriteLine(string.Format("Completion status: {0}.", t.Status));
}).Wait();
}
示例4: SetupPipeline
public static ITargetBlock<string> SetupPipeline()
{
var fileNamesForPath = new TransformBlock<string, IEnumerable<string>>(
path =>
{
return GetFileNames(path);
});
var lines = new TransformBlock<IEnumerable<string>, IEnumerable<string>>(
fileNames =>
{
return LoadLines(fileNames);
});
var words = new TransformBlock<IEnumerable<string>, IEnumerable<string>>(
lines2 =>
{
return GetWords(lines2);
});
var display = new ActionBlock<IEnumerable<string>>(
coll =>
{
foreach (var s in coll)
{
WriteLine(s);
}
});
fileNamesForPath.LinkTo(lines);
lines.LinkTo(words);
words.LinkTo(display);
return fileNamesForPath;
}
示例5: Run
public void Run()
{
var cts = new CancellationTokenSource();
Console.WriteLine("Generating first {0} powers of 2.", MaxItems);
var bufferBlock = new BufferBlock<int>(new DataflowBlockOptions { CancellationToken = cts.Token });
Enumerable.Range(1, MaxItems)
.ToList()
.ForEach(i => bufferBlock.Post(i));
Console.WriteLine("Scheduling cancellation after 5 seconds.");
cts.CancelAfter(TimeSpan.FromSeconds(5));
Console.WriteLine("Creating and linking the remaing blocks to the network.");
var transformBlock = new TransformBlock<int, double>(i =>
{
Thread.Sleep(500);
return Math.Pow(2, i);
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 1, CancellationToken = cts.Token });
var actionBlock = new ActionBlock<double>(async i =>
{
await Task.Delay(1000);
Console.WriteLine(i);
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 10, CancellationToken = cts.Token });
bufferBlock.LinkTo(transformBlock, new DataflowLinkOptions { PropagateCompletion = true });
transformBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true });
var t1 = bufferBlock.Completion.ContinueWith(t => Console.WriteLine("Buffer block status: {0}", t.Status));
var t2 = actionBlock.Completion.ContinueWith(t => Console.WriteLine("Action block status: {0}", t.Status));
Console.WriteLine("Waiting for the network to finish.");
Task.WaitAll(t1, t2);
}
示例6: Run
public void Run()
{
Console.WriteLine("Generating first {0} powers of 2.", MaxItems);
var bufferBlock = new BufferBlock<int>();
var transformBlock = new TransformBlock<int, double>(i =>
{
Thread.Sleep(500);
return Math.Pow(2, i);
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 10 });
var actionBlock = new ActionBlock<double>(async i =>
{
await Task.Delay(1000);
Console.WriteLine(i);
_waitHandle.Signal();
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 10 });
bufferBlock.LinkTo(transformBlock);
transformBlock.LinkTo(actionBlock);
Enumerable.Range(1, MaxItems)
.ToList()
.ForEach(i => bufferBlock.Post(i));
_waitHandle.Wait();
}
示例7: MessagePipeline
public MessagePipeline()
{
linkOptions = new DataflowLinkOptions { PropagateCompletion = true };
buildMessage = new TransformBlock<object, Messaging.Message>(
x => {
Console.WriteLine("buildMessage| message: {0}", x);
return new Messaging.Message { Body = x };
});
logMessage = new TransformBlock<Messaging.Message, Messaging.Message>
(x => {
Console.WriteLine("logMessage| MessageId: {0}. Body: {1}.", x.MessageId, x.Body);
return x;
});
sendMessage = new TransformBlock<Messaging.Message, Messaging.Message>(
x => {
Console.WriteLine("sendMessage| MessageId: {0}. Body: {1}.", x.MessageId, x.Body);
return x;
});
buildMessage.LinkTo(logMessage, linkOptions);
logMessage.LinkTo(sendMessage, linkOptions);
}
示例8: ValidateBlockAsync
public static async Task ValidateBlockAsync(ICoreStorage coreStorage, ICoreRules rules, Chain newChain, ISourceBlock<ValidatableTx> validatableTxes, CancellationToken cancelToken = default(CancellationToken))
{
// tally transactions
object finalTally = null;
var txTallier = new TransformBlock<ValidatableTx, ValidatableTx>(
validatableTx =>
{
var runningTally = finalTally;
rules.TallyTransaction(newChain, validatableTx, ref runningTally);
finalTally = runningTally;
return validatableTx;
});
validatableTxes.LinkTo(txTallier, new DataflowLinkOptions { PropagateCompletion = true });
// validate transactions
var txValidator = InitTxValidator(rules, newChain, cancelToken);
// begin feeding the tx validator
txTallier.LinkTo(txValidator, new DataflowLinkOptions { PropagateCompletion = true });
// validate scripts
var scriptValidator = InitScriptValidator(rules, newChain, cancelToken);
// begin feeding the script validator
txValidator.LinkTo(scriptValidator, new DataflowLinkOptions { PropagateCompletion = true });
//TODO
await PipelineCompletion.Create(
new Task[] { },
new IDataflowBlock[] { validatableTxes, txTallier, txValidator, scriptValidator });
// validate overall block
rules.PostValidateBlock(newChain, finalTally);
}
示例9: Start
public void Start()
{
var sink = new ActionBlock<PageResultMessage>((Action<PageResultMessage>)Sink);
var source = new BufferBlock<GetPageMessage>();
var linkOptions = new DataflowLinkOptions {PropagateCompletion = false};
for (int i = 0; i < 10; i++)
{
var options = new ExecutionDataflowBlockOptions
{
BoundedCapacity = 1
};
var worker = new TransformBlock<GetPageMessage, PageResultMessage>(
(Func<GetPageMessage, PageResultMessage>)Worker, options);
source.LinkTo(worker, linkOptions);
worker.LinkTo(sink, linkOptions);
}
foreach (var url in UrlList.Urls)
{
source.Post(new GetPageMessage{ Url = url });
}
source.Complete();
sink.Completion.Wait();
}
示例10: BatchedInsert
public BatchedInsert()
{
Output = new TransformBlock<object, List<object>>(obj =>
{
if (objects.Count > BatchSize)
objects.Clear();
if(objects.Count < BatchSize)
{
objects.Add(obj);
return null;
}
return objects;
});
DatabaseInserter = new ActionBlock<object>(objs =>
{
if (objs == null)
return;
var toInsert = (List<object>)objs;
toInsert = new List<object>(toInsert);
Mapping.Insert(toInsert);
});
(Output as TransformBlock<object, List<object>>).LinkTo(DatabaseInserter);
}
示例11: DiskParallelProbingAsync
/// <summary>
/// Search Asynchrony many extension in all of Fixed and Removable Disks.
/// </summary>
/// <param name="targetExtensions">Some file extensions for use search pattern.</param>
/// <example>
/// FileExtension example:
/// {".jpg", 646546Byte, 646Byte}
/// {".pdf", 25464645546Byte, 60000Byte}
/// </example>
/// <returns>A sorted list of detected files</returns>
public static async Task<List<FileInfo>> DiskParallelProbingAsync(List<FileExtensionOption> targetExtensions, System.Threading.CancellationTokenSource CTS)
{
return await Task.Run(() =>
{
searchComplete = false;
//
Reporter("DiskProbing", new ReportEventArgs("DiskProbing", ReportCodes.DiskProbingStarted, "---{Search Disks Started}---"));
List<FileInfo> _result = new List<FileInfo>();
//
// Find specific folders from windows drives instead of the total drive.
//
FolderInfo[] SpecificsDirectory = CheckDirectoriesChanges.GetDirectoriesInformation();
//
// Set Data-flow
//
TransformBlock<FolderInfo, List<FileInfo>> TB = new TransformBlock<FolderInfo, List<FileInfo>>(dir =>
{
Reporter(dir, new ReportEventArgs("DiskProbing",
ReportCodes.TheSearchBeginning,
"Searching {0} ...", dir.FullName));
List<FileInfo> res = dir.GetDirectoryInfo.SearchDirectory(targetExtensions, CTS);
Reporter(dir, new ReportEventArgs("DiskProbing",
ReportCodes.TheSearchCompleted,
"The Search {0} was completed!", dir.FullName));
return res;
}, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount });
ActionBlock<List<FileInfo>> AB = new ActionBlock<List<FileInfo>>(lst => _result.AddRange(lst));
//
// Search specific folders from windows drives instead of the total drive.
//
try
{
TB.LinkTo(AB);
ParallelOptions opt = new ParallelOptions() { CancellationToken = CTS.Token, MaxDegreeOfParallelism = Environment.ProcessorCount };
var pLoop = Parallel.ForEach(SpecificsDirectory, opt, async dir => await TB.SendAsync(dir));
TB.Complete();
TB.Completion.Wait();
}
catch (Exception ex) { Reporter(SpecificsDirectory, new ReportEventArgs("SearchEngine.DiskProbing.SpecificsDirectory", ex)); }
searchComplete = true;
Reporter("DiskProbing", new ReportEventArgs("DiskProbing",
ReportCodes.DiskProbingFinished,
"---{Search Disks Finished}---"));
LastSearchResult = _result;
return _result;
});
}
示例12: ReplayRollbackUtxo
public static ISourceBlock<ValidatableTx> ReplayRollbackUtxo(ICoreStorage coreStorage, IChainState chainState, ChainedHeader replayBlock, CancellationToken cancelToken = default(CancellationToken))
{
// replaying rollback of an on-chain block, use the chainstate tx index for replay, same as replaying forward
if (chainState.Chain.BlocksByHash.ContainsKey(replayBlock.Hash))
{
return ReplayFromTxIndex(coreStorage, chainState, replayBlock, replayForward: false, cancelToken: cancelToken);
}
// replaying rollback of an off-chain (re-org) block, use the unminted information for replay
else
{
IImmutableList<UnmintedTx> unmintedTxesList;
if (!chainState.TryGetBlockUnmintedTxes(replayBlock.Hash, out unmintedTxesList))
{
//TODO if a wallet/monitor were to see a chainstate block that wasn't flushed to disk yet,
//TODO and if bitsharp crashed, and if the block was orphaned: then the orphaned block would
//TODO not be present in the chainstate, and it would not get rolled back to generate unminted information.
//TODO DeferredChainStateCursor should be used in order to re-org the chainstate in memory and calculate the unminted information
throw new MissingDataException(replayBlock.Hash);
}
var unmintedTxes = ImmutableDictionary.CreateRange(
unmintedTxesList.Select(x => new KeyValuePair<UInt256, UnmintedTx>(x.TxHash, x)));
var lookupLoadingTx = new TransformBlock<DecodedBlockTx, ValidatableTx>(
blockTx =>
{
var tx = blockTx.Transaction;
var txIndex = blockTx.Index;
var prevTxOutputs = ImmutableArray.CreateBuilder<PrevTxOutput>(!blockTx.IsCoinbase ? tx.Inputs.Length : 0);
if (!blockTx.IsCoinbase)
{
UnmintedTx unmintedTx;
if (!unmintedTxes.TryGetValue(tx.Hash, out unmintedTx))
throw new MissingDataException(replayBlock.Hash);
prevTxOutputs.AddRange(unmintedTx.PrevTxOutputs);
}
return new ValidatableTx(blockTx, replayBlock, prevTxOutputs.MoveToImmutable());
});
IEnumerator<BlockTx> blockTxes;
if (!coreStorage.TryReadBlockTransactions(replayBlock.Hash, out blockTxes))
{
throw new MissingDataException(replayBlock.Hash);
}
var blockTxesBuffer = new BufferBlock<DecodedBlockTx>();
blockTxesBuffer.LinkTo(lookupLoadingTx, new DataflowLinkOptions { PropagateCompletion = true });
blockTxesBuffer.SendAndCompleteAsync(blockTxes.UsingAsEnumerable().Select(x => x.Decode()).Reverse(), cancelToken).Forget();
return lookupLoadingTx;
}
}
示例13: ProcessingByTPL_StraightForwardImplementation
static public void ProcessingByTPL_StraightForwardImplementation()
{
const string pathToFiles = @"..\..\..\..\DataFiles";
string[] files = Directory.GetFiles(pathToFiles, "*.txt", SearchOption.AllDirectories);
var loadDataFromFileBlock = new TransformBlock<string[], List<CustomerTextData>>(fileItems =>
{
var factory = new CustomerTextDataFactory();
return new List<CustomerTextData>(Array.ConvertAll(fileItems, factory.LoadFromFile));
});
var filterBlock = new TransformBlock<List<CustomerTextData>, List<CustomerTextData>>(textDataList =>
{
var filter = new FilterTextData(5);
return textDataList.Where(filter.Run).ToList();
});
var toListBlock = new TransformManyBlock<List<CustomerTextData>, CustomerTextData>(textDataList =>
{
var queue = new ConcurrentQueue<CustomerTextData>();
textDataList.ForEach(queue.Enqueue);
return queue;
});
var action = new ActionBlock<CustomerTextData>(textData =>
{
var weight = new WeightTextData();
int result = weight.Run(textData);
Trace.WriteLine(result);
Console.WriteLine(result);
});
loadDataFromFileBlock.LinkTo(filterBlock);
filterBlock.LinkTo(toListBlock);
toListBlock.LinkTo(action);
loadDataFromFileBlock.Completion.ContinueWith(t =>
{
if (t.IsFaulted) ((IDataflowBlock)filterBlock).Fault(t.Exception);
else filterBlock.Complete();
});
filterBlock.Completion.ContinueWith(t =>
{
if (t.IsFaulted) ((IDataflowBlock)toListBlock).Fault(t.Exception);
else toListBlock.Complete();
});
toListBlock.Completion.ContinueWith(t =>
{
if (t.IsFaulted) ((IDataflowBlock)action).Fault(t.Exception);
else action.Complete();
});
loadDataFromFileBlock.Post(files);
loadDataFromFileBlock.Complete();
action.Completion.Wait();
}
示例14: Main
static void Main(string[] args)
{
string s =
"http://cn.bing.com/search?q=MD5CryptoServiceProvider+slow&qs=n&pq=md5cryptoserviceprovider+slow&sc=0-25&sp=-1&sk=&cvid=67d40cbd8c424d55a3db83e6e9868267&first=51&FORM=PERE4";
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] inBytes = Encoding.UTF8.GetBytes(s);
var bytes = md5.ComputeHash(inBytes);
Console.WriteLine(bytes.Length);
}
var splitter = new TransformBlock<string, KeyValuePair<string, int>>(
input =>
{
var splitted = input.Split('=');
return new KeyValuePair<string, int>(splitted[0], int.Parse(splitted[1]));
});
var dict = new Dictionary<string, int>();
var aggregater = new ActionBlock<KeyValuePair<string, int>>(
pair =>
{
int oldValue;
dict[pair.Key] = dict.TryGetValue(pair.Key, out oldValue) ? oldValue + pair.Value : pair.Value;
});
splitter.LinkTo(aggregater, new DataflowLinkOptions() { PropagateCompletion = true});
splitter.Post("a=1");
splitter.Post("b=2");
splitter.Post("a=5");
splitter.Complete();
aggregater.Completion.Wait();
Console.WriteLine("sum(a) = {0}", dict["a"]); //prints sum(a) = 6
//CalcAsync().Wait();
//SlowFlowAsync().Wait();
//FailDemoAsync().Wait();
//TransformAndLinkDemo().Wait();
//LinkLeftToDemo().Wait();
//CircularFlowAutoComplete().Wait();
//RecorderDemo().Wait();
BulkInserterDemo().Wait();
//BulkInserterDemo2().Wait();
//BroadcasterDemo().Wait();
//MyLoggerDemo().Wait();
//ETLLookupDemo().Wait();
}
示例15: TransformThroughFilterToAction
public async Task TransformThroughFilterToAction()
{
int completedCount = 0;
var t = new TransformBlock<int, int>(i => i);
var c = new ActionBlock<int>(i => completedCount++);
t.LinkTo(c, new DataflowLinkOptions { PropagateCompletion = true }, i => true);
t.PostRange(0, Iterations);
t.Complete();
await c.Completion;
Assert.Equal(expected: Iterations, actual: completedCount);
}