本文整理汇总了C#中Channel.Receive方法的典型用法代码示例。如果您正苦于以下问题:C# Channel.Receive方法的具体用法?C# Channel.Receive怎么用?C# Channel.Receive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Channel
的用法示例。
在下文中一共展示了Channel.Receive方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static async Task Run()
{
var result = new Channel<int>();
var messages = new Channel<string>();
Go.Run(MultiplyAsync, 10, 20, result, messages);
WriteLine($"result was {await result.Receive().ConfigureAwait(false)}");
WriteLine($"message was {await messages.Receive().ConfigureAwait(false)}");
result.Receive().Wait();
messages.Receive().Wait();
}
示例2: SendAndReceiveBlocking_1item
public void SendAndReceiveBlocking_1item()
{
var c = new Channel<int>();
var hits = new List<int>();
var t = Task.Run(async () => {
var x = await c.Receive();
hits.Add(x);
});
Assert.AreEqual(0, hits.Count);
c.Send(1).Wait();
t.Wait();
CollectionAssert.AreEqual(new[] { 1 }, hits);
}
示例3: SendAndReceiveBlocking_Sequence
public void SendAndReceiveBlocking_Sequence()
{
var c = new Channel<int>();
var hits = new List<int>();
var t = Task.Run(() => {
for (int i = 0; i < 5; i++) {
hits.Add(c.Receive().Result);
}
});
Assert.AreEqual(0, hits.Count);
c.Send(1).Wait();
c.Send(2).Wait();
c.Send(3).Wait();
c.Send(4).Wait();
c.Send(5).Wait();
t.Wait();
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5 }, hits);
}
示例4: ReceiveOnClosedChannelReturnsDefault
public void ReceiveOnClosedChannelReturnsDefault()
{
var ci = new Channel<int>();
ci.Close();
Assert.AreEqual(0, ci.Receive().Result);
var cs = new Channel<string>();
cs.Close();
Assert.AreEqual(null, cs.Receive().Result);
}
示例5: SendAndReceiveBlocking_ManyReceivers
public void SendAndReceiveBlocking_ManyReceivers()
{
var c = new Channel<int>();
var hits = new List<int>();
var tasks = new List<Task>(5);
for (int i = 0; i < 5; i++) {
tasks.Add(Task.Run(() => {
hits.Add(c.Receive().Result);
}));
}
Assert.AreEqual(0, hits.Count);
var _ = c.Send(1);
// the first task to call Receive will complete first, but we can't control
// the task launch order so we don't know which task that might be
tasks.Remove(Task.WhenAny(tasks).Result);
CollectionAssert.AreEqual(new[] { 1 }, hits);
_ = c.Send(2);
tasks.Remove(Task.WhenAny(tasks).Result);
CollectionAssert.AreEqual(new[] { 1, 2 }, hits);
_ = c.Send(3);
tasks.Remove(Task.WhenAny(tasks).Result);
CollectionAssert.AreEqual(new[] { 1, 2, 3 }, hits);
_ = c.Send(4);
tasks.Remove(Task.WhenAny(tasks).Result);
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 }, hits);
_ = c.Send(5);
tasks.Remove(Task.WhenAny(tasks).Result);
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5 }, hits);
Assert.AreEqual(0, tasks.Count);
}
示例6: Sieve
/// <summary>
/// Creates a prime sieve.
/// </summary>
/// <returns>prime sieve (a channel)</returns>
public static Channel<int> Sieve()
{
var c = new Channel<int>();
Task.Factory.StartNew(Counter, c);
var prime = new Channel<int>();
Task.Factory.StartNew(() =>
{
int p;
Channel<int> newChan;
for (; ; )
{
p = c.Receive();
prime.Send(p);
newChan = new Channel<int>();
var arg = new FilterArg();
arg.Prime = p;
arg.Listener = c;
arg.Sender = newChan;
Task.Factory.StartNew(Filter, arg, TaskCreationOptions.LongRunning);
c = newChan;
}
});
return prime;
}
示例7: SendAndReceiveBlocking_ManyReceivers_ArrivingOneAtATime
public void SendAndReceiveBlocking_ManyReceivers_ArrivingOneAtATime()
{
var c = new Channel<int>();
var hits = new List<int>();
Func<Task> addReceiver = () => Task.Run(() => hits.Add(c.Receive().Result));
Assert.AreEqual(0, hits.Count);
var t = addReceiver();
var _ = c.Send(1);
t.Wait();
CollectionAssert.AreEqual(new[] { 1 }, hits);
t = addReceiver();
_ = c.Send(2);
t.Wait();
CollectionAssert.AreEqual(new[] { 1, 2 }, hits);
t = addReceiver();
_ = c.Send(3);
t.Wait();
CollectionAssert.AreEqual(new[] { 1, 2, 3 }, hits);
t = addReceiver();
_ = c.Send(4);
t.Wait();
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 }, hits);
t = addReceiver();
_ = c.Send(5);
t.Wait();
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5 }, hits);
}
示例8: ChannelsPingPongBackAndForth
public async Task ChannelsPingPongBackAndForth()
{
var left = new Channel<int>();
var right = new Channel<int>();
var _ = Task.Run(async () => {
for (int i = 0; i < 5; i++) {
var x = await left.Receive().ConfigureAwait(false);
await right.Send(x);
}
});
for(int i = 0; i < 5; i++) {
await left.Send(i).ConfigureAwait(false);
var x = await right.Receive();
Assert.AreEqual(i, x);
}
}
示例9: SendAndReceiveBlocking_ManySenders
public void SendAndReceiveBlocking_ManySenders()
{
var c = new Channel<int>();
for (int i = 0; i < 5; i++) {
var monoDoesntHaveNewLoopScope = i;
Task.Run(() => {
c.Send(monoDoesntHaveNewLoopScope + 1).Wait();
});
}
var hits = new List<int>();
for (int i = 0; i < 5; i++) {
hits.Add(c.Receive().Result);
}
hits.Sort(); // 5 tasks run in random order
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5 }, hits);
}