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


C# Channel.Send方法代码示例

本文整理汇总了C#中Channel.Send方法的典型用法代码示例。如果您正苦于以下问题:C# Channel.Send方法的具体用法?C# Channel.Send怎么用?C# Channel.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Channel的用法示例。


在下文中一共展示了Channel.Send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: can_read_after_select_on_queued_Channels

 public void can_read_after_select_on_queued_Channels()
 {
     var ch1 = new Channel<int>(1);
     var ch2 = new Channel<bool>(1);
     ThreadPool.QueueUserWorkItem(state => {
         ch1.Send(123);
         ch2.Send(true);
         ch2.Close();
         ch1.Send(124);
         ch1.Close();
     });
     using (var select = new Channels(Op.Recv(ch1), Op.Recv(ch2))) {
         var got = select.Select();
         Debug.Print("got.Index = " + got.Index);
         if (got.Index == 0) {
             Assert.AreEqual(123, got.Value, "got.Value");
             Assert.AreEqual(Maybe<bool>.Some(true), ch2.Recv());
         }
         else {
             Assert.AreEqual(1, got.Index, "got.Index");
             Assert.AreEqual(true, got.Value, "got.Value");
             Assert.AreEqual(Maybe<int>.Some(123), ch1.Recv());
         }
         select.ClearAt(1);
         got = select.Select();
         Assert.AreEqual(0, got.Index, "got.Index, value =" + got.Value);
         Assert.AreEqual(124, got.Value, "got.Value");
     }
 }
开发者ID:busterwood,项目名称:NetChan,代码行数:29,代码来源:SelectTests.cs

示例2: can_enumerate_multiple_items

 public void can_enumerate_multiple_items()
 {
     var ch = new Channel<int>(1);
     ThreadPool.QueueUserWorkItem(state => { ch.Send(123); ch.Send(124); ch.Close(); });
     var e = ch.GetEnumerator();
     Assert.IsTrue(e.MoveNext());
     Assert.AreEqual(123, e.Current);
     Assert.IsTrue(e.MoveNext());
     Assert.AreEqual(124, e.Current);
     Assert.IsFalse(e.MoveNext(), "final move next:" + e.Current);
 }
开发者ID:busterwood,项目名称:NetChan,代码行数:11,代码来源:QueuedChannelTests.cs

示例3: FanOutIn

    public static async Task FanOutIn()
    {
        var numbers = new Channel<int>();
        var letters = new Channel<char>();

        Go.Run(async () => {
            for (int i = 0; i < 10; i++)
                await numbers.Send(i);

            Console.WriteLine("numbers all sent");
            numbers.Close();
        });

        Go.Run(async () => {
            for (int i = 0; i < 10; i++)
            {
                await letters.Send((char)(i + 97));
            }

            Console.WriteLine("letters all sent");
            letters.Close();
        });

        while(numbers.IsOpen || letters.IsOpen)
        {
            await Go.Select(
                Go.Case(numbers, num => {
                    Console.WriteLine($"Got {num}");
                }),
                Go.Case(letters, ch => {
                    Console.WriteLine($"Got {ch}");
                }));
        }
    }
开发者ID:Porges,项目名称:Ignite2015,代码行数:34,代码来源:goroutines_main.cs

示例4: can_recv_one_item_before_closing

 public void can_recv_one_item_before_closing()
 {
     var ch = new Channel<int>(1);
     ThreadPool.QueueUserWorkItem(state => { ch.Send(123); ch.Close(); });
     Assert.AreEqual(Maybe<int>.Some(123), ch.Recv());
     Assert.AreEqual(Maybe<int>.None(), ch.TryRecv());
 }
开发者ID:busterwood,项目名称:NetChan,代码行数:7,代码来源:QueuedChannelTests.cs

示例5: Ping

			public void Ping(Channel<Ping> channel)
			{
				channel.Send(new Ping
					{
						ResponseChannel = PongChannel
					});
			}
开发者ID:Nangal,项目名称:Stact,代码行数:7,代码来源:PingPong_Specs.cs

示例6: can_read_after_select

 public void can_read_after_select()
 {
     var ch1 = new Channel<int>();
     var ch2 = new Channel<bool>();
     ThreadPool.QueueUserWorkItem(state => {
         ch1.Send(123);
         ch2.Send(true);
         ch1.Send(124);
     });
     using (var select = new Channels(Op.Recv(ch1), Op.Recv(ch2))) {
         var got = select.Select();
         Assert.AreEqual(0, got.Index, "select.Index");
         Assert.AreEqual(123, got.Value, "select.Value");
         Assert.AreEqual(Maybe<bool>.Some(true), ch2.Recv());
         Assert.AreEqual(Maybe<int>.Some(124), ch1.Recv());
     }
 }
开发者ID:busterwood,项目名称:NetChan,代码行数:17,代码来源:SelectTests.cs

示例7: 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

示例8: Enumerate

        public void Enumerate()
        {
            Channel<int> channel = new Channel<int>();

            for (int i = 0; i < 10; i++)
                channel.Send(i);

            IChannelEnumerable<int> enumerable = new ChannelEnumerable<int>(channel);

            int n = 0;
            foreach(int i in enumerable.Take(10))
                Expect(i, EqualTo(n++));
        }
开发者ID:Noah1989,项目名称:fmacj,代码行数:13,代码来源:ChannelEnumerableTest.cs

示例9: 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

示例10: 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

示例11: CalcCrc32

	static async Task CalcCrc32(string filePath, Channel<CrcResult> results, Channel<Exception> errors, WaitGroup wg) {
        try
        {
            var buffer = File.ReadAllBytes(filePath);
            if (buffer.Length < 1) // simulate an exception
            {
                await errors.Send(new Exception($"0 byte file at {filePath}"));
                return;
            }

            await results.Send(new CrcResult {
                Value = DamienG.Security.Cryptography.Crc32.Compute(buffer),
                Path = filePath
            });
        }
        catch(Exception e)
        {
            await errors.Send(e);
        }
        finally
        {
            wg.Done();
        }
	}
开发者ID:Porges,项目名称:Ignite2015,代码行数:24,代码来源:crc_concurrent.cs

示例12: ManyValues

        public void ManyValues()
        {
            Channel<int> channel1 = new Channel<int>();
            Channel<double> channel2 = new Channel<double>();

            for (int i = 1; i <= 1000; i++)
            {
                channel1.Send(i);
                channel2.Send(-i*0.5);
            }
            Thread.Sleep(100);

            List<int> result1 = new List<int>();
            List<double> result2 = new List<double>();

            var bus = new Bus(new IChannel[] {channel1, channel2},
                              new ChannelOptions[] {
                              	  new ChannelOptions(false),
                                  new ChannelOptions(false)});

            for (int i = 1; i <= 1000; i++)
            {

                object[] objects = bus.Receive();
                Expect(objects.Length, EqualTo(2));
                result1.Add((int) objects[0]);
                result2.Add((double) objects[1]);
            }
            bus.Close();

            for (int i = 1; i <= 1000; i++)
                Expect(result1.Contains(i), String.Format("Missing number: {0} ({1} received)", i, result1.Count));

            for (int i = 1; i <= 1000; i++)
                Expect(result2.Contains(-i*0.5), String.Format("Missing number: {0} ({1} received)", i, result1.Count));
        }
开发者ID:Noah1989,项目名称:fmacj,代码行数:36,代码来源:BusTest.cs

示例13: 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

示例14: cannot_send_on_a_closed_Channel

 public void cannot_send_on_a_closed_Channel()
 {
     var ch = new Channel<int>(1);
     ch.Close();
     ch.Send(123);
 }
开发者ID:busterwood,项目名称:NetChan,代码行数:6,代码来源:QueuedChannelTests.cs

示例15: send_does_not_block_when_capacity_spare

 public void send_does_not_block_when_capacity_spare()
 {
     var ch = new Channel<bool>(1);
     var start = Environment.TickCount;
     ThreadPool.QueueUserWorkItem(state => { Thread.Sleep(50); ch.Recv(); });
     ch.Send(true);
     var elapsed = Environment.TickCount - start;
     Assert.IsTrue(elapsed < 50, "Elapsed " + elapsed);
 }
开发者ID:busterwood,项目名称:NetChan,代码行数:9,代码来源:QueuedChannelTests.cs


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