当前位置: 首页>>代码示例>>C#>>正文


C# Channel.Receive方法代码示例

本文整理汇总了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();
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:13,代码来源:basic.cs

示例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);
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:15,代码来源:ChannelTest.cs

示例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);
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:21,代码来源:ChannelTest.cs

示例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);
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:10,代码来源:ChannelTest.cs

示例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);
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:37,代码来源:ChannelTest.cs

示例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;
 }
开发者ID:rosado,项目名称:prime-sieve-cs,代码行数:28,代码来源:Program.cs

示例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);
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:33,代码来源:ChannelTest.cs

示例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);
        }
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:18,代码来源:ChannelTest.cs

示例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);
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:19,代码来源:ChannelTest.cs


注:本文中的Channel.Receive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。