本文整理汇总了C#中TransformBlock.Post方法的典型用法代码示例。如果您正苦于以下问题:C# TransformBlock.Post方法的具体用法?C# TransformBlock.Post怎么用?C# TransformBlock.Post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TransformBlock
的用法示例。
在下文中一共展示了TransformBlock.Post方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例2: 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();
}
示例3: ConstructTransformWithNMessages
private static TransformBlock<int, string> ConstructTransformWithNMessages(int messagesCount)
{
var block = new TransformBlock<int, string>(i => i.ToString());
for (int i = 0; i < messagesCount; i++)
{
block.Post(i);
}
// Spin until the messages have been properly buffered up.
// Otherwise TryReceive fails.
SpinWait.SpinUntil(() => block.OutputCount == messagesCount);
return block;
}
示例4: DeferredUsageTest
public void DeferredUsageTest ()
{
int[] array = new int[10];
var action = new ActionBlock<int> (i => array[Math.Abs (i)] = i);
var block = new TransformBlock<int, int> (i => -i);
for (int i = 0; i < array.Length; ++i)
Assert.IsTrue (block.Post (i), "Not accepted");
Thread.Sleep (300);
block.LinkTo (action);
Thread.Sleep (100);
CollectionAssert.AreEqual (new[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
}
示例5: BasicUsageTest
public void BasicUsageTest ()
{
int[] array = new int[10];
var evt = new ManualResetEventSlim (false);
ActionBlock<int> action = new ActionBlock<int> ((i) => { array[Math.Abs (i)] = i; evt.Set (); });
TransformBlock<int, int> block = new TransformBlock<int, int> (i => -i);
block.LinkTo (action);
for (int i = 0; i < array.Length; ++i)
Assert.IsTrue (block.Post (i), "Not accepted");
evt.Wait ();
CollectionAssert.AreEqual (new int[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
}
示例6: AsyncNullTest
public void AsyncNullTest ()
{
var scheduler = new TestScheduler ();
var block = new TransformBlock<int, int> (
i => null,
new ExecutionDataflowBlockOptions { TaskScheduler = scheduler });
Assert.IsTrue (block.Post (1));
scheduler.ExecuteAll ();
Assert.IsFalse (block.Completion.Wait (100));
block.Complete ();
Assert.IsTrue (block.Completion.Wait (100));
}
示例7: TransformThroughFilterToAction
internal static bool TransformThroughFilterToAction()
{
const int ITERS = 2;
int completedCount = 0;
var t = new TransformBlock<int, int>(i => i);
var c = new ActionBlock<int>(i => completedCount++);
t.LinkTo(c, i => true);
t.Completion.ContinueWith(_ => c.Complete());
for (int i = 0; i < ITERS; i++) t.Post(i);
t.Complete();
c.Completion.Wait();
return completedCount == ITERS;
}
示例8: Example1
public static void Example1()
{
var conf = new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 4 };
ActionBlock<int> a = new ActionBlock<int>(i =>
{
Thread.Sleep(500);
Console.WriteLine(i);
}, conf);
TransformBlock<int, int> t = new TransformBlock<int, int>(i => i * 3);
t.LinkTo(a);
for (int i = 0; i < 12; i++)
{
t.Post(i);
}
}
示例9: Run
public void Run()
{
var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 8 };
var tb = new TransformBlock<int, int>(i => i * 2);
var ab = new ActionBlock<int>(i => this.Compute(i), options);
tb.LinkTo(ab);
for (var i = 0; i < 10; i++)
{
tb.Post(i);
}
tb.Complete();
tb.Completion.Wait();
Thread.Sleep(500);
}
示例10: TestBlockBufferCount
public async Task TestBlockBufferCount()
{
var block1 = new TransformBlock<int, int>(i => 2 * i);
var block2 = new TransformManyBlock<int, int>(i => new [] { i });
var block3 = new ActionBlock<int>(i => { Thread.Sleep(1000); });
block1.Post(0);
block2.Post(0);
block2.Post(0);
block3.Post(0);
block3.Post(0);
block3.Post(0);
await Task.Delay(200);
Assert.AreEqual(1, block1.GetBufferCount().Total());
Assert.AreEqual(2, block2.GetBufferCount().Total());
Assert.AreEqual(2, block3.GetBufferCount().Total());
}
示例11: BasicUsageTest
public void BasicUsageTest ()
{
int[] array = new int[10];
var evt = new CountdownEvent (10);
var action = new ActionBlock<int> (i =>
{
array [Math.Abs (i)] = i;
evt.Signal ();
});
var block = new TransformBlock<int, int> (i => -i);
block.LinkTo (action);
for (int i = 0; i < array.Length; ++i)
Assert.IsTrue (block.Post (i), "Not accepted");
evt.Wait ();
CollectionAssert.AreEqual (
new[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }, array);
}
示例12: TransformToAction
internal static bool TransformToAction()
{
bool passed = true;
const int ITERS = 2;
var t = new TransformBlock<int, int>(i => i * 2);
int completedCount = 0;
int prev = -2;
var c = new ActionBlock<int>(i =>
{
completedCount++;
if (i != prev + 2) passed &= false;
prev = i;
});
t.LinkWithCompletion(c);
for (int i = 0; i < ITERS; i++) t.Post(i);
t.Complete();
c.Completion.Wait();
Assert.True(completedCount == ITERS);
return passed;
}
示例13: TData
public async static void TData()
{
var multiplyBlock = new TransformBlock<int, int>(item =>
{
var res = item * 2;
Console.WriteLine("{0} * 2 = {1}", item, res);
return res;
});
var divideBlock = new TransformBlock<int, int>(item =>
{
var res = item / 2;
Console.WriteLine("{0} / 2 = {1}", item, res);
return res;
});
multiplyBlock.LinkTo(divideBlock);
multiplyBlock.Post(2);
multiplyBlock.Complete();
await divideBlock.Completion;
}
示例14: Main
static void Main(string[] args)
{
var multiplyBlock = new TransformBlock<int, int>(value => value * 2);
var subtractBlock = new TransformBlock<int, int>(value => value - 2);
var displayBlock = new ActionBlock<int>(value => Console.WriteLine(value));
// multiplyBlock ==> subtractBlock ==> displayBlock
var linkOptions = new DataflowLinkOptions { PropagateCompletion = true };
multiplyBlock.LinkTo(subtractBlock, linkOptions);
subtractBlock.LinkTo(displayBlock, linkOptions);
// Put data in the first block (multiplyBlock)
foreach (var i in Enumerable.Range(0, 10))
multiplyBlock.Post(i);
// Mark it as complete. Completion will propagate because of the link options.
multiplyBlock.Complete();
// Wait for the last block (displayBlock) to complete.
displayBlock.Completion.Wait();
Console.ReadKey();
}
示例15: TestQuickStop
private static bool TestQuickStop(bool testThrow)
{
bool passed = true;
CancellationTokenSource cts = new CancellationTokenSource();
var options = new ExecutionDataflowBlockOptions { CancellationToken = cts.Token };
var propagator = new TransformBlock<ThrowOn, ThrowOn>(x => { Task.Delay(200).Wait(); return x; }, options);
var thrower = new ThrowerBlock();
// Post enough messages to require long processing
for (int i = 0; i < 2; i++)
propagator.Post(testThrow && i == 1 ? ThrowOn.OfferMessage : ThrowOn.TryReceive); // Throw on the second message
// Link the thrower
propagator.LinkTo(thrower);
// Once a message has been processed, cancel the propagator (if we are testing cancellation)
SpinWait.SpinUntil(() => thrower.LastOperation == ThrowOn.OfferMessage);
if (!testThrow) cts.Cancel();
// Wait for the propagator to complete
try
{
var ranToCompletion = propagator.Completion.Wait(10000);
passed = false;
Console.WriteLine("Task is faulted or canceled (finished: {0}) - FAILED", ranToCompletion ? "ran to copmpletion" : "still running");
}
catch (AggregateException ae)
{
passed = testThrow ? ae.InnerException is InvalidOperationException : ae.InnerException is TaskCanceledException;
ae.Handle(e => true);
Console.WriteLine("Task is faulted or canceled (exception) - {0}", passed ? "Passed" : "FAILED");
}
return passed;
}