本文整理汇总了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");
}
}
示例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);
}
示例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}");
}));
}
}
示例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());
}
示例5: Ping
public void Ping(Channel<Ping> channel)
{
channel.Send(new Ping
{
ResponseChannel = PongChannel
});
}
示例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());
}
}
示例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);
}
示例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++));
}
示例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);
}
示例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);
}
示例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();
}
}
示例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));
}
示例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;
}
示例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);
}
示例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);
}