本文整理汇总了C#中Channels.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Channels.Select方法的具体用法?C# Channels.Select怎么用?C# Channels.Select使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Channels
的用法示例。
在下文中一共展示了Channels.Select方法的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_select_on_all_closed_Channels
public void can_select_on_all_closed_Channels()
{
var ch1 = new Channel<int>();
var ch2 = new Channel<bool>();
ch1.Close();
ch2.Close();
using (var select = new Channels(Op.Recv(ch1), Op.Recv(ch2))) {
var got = select.Select();
Assert.AreNotEqual(-1, got.Index, "expected any channel to return");
Assert.AreEqual(null, got.Value);
}
}
示例3: 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());
}
}
示例4: read_second_of_multiple_Channels
public void read_second_of_multiple_Channels()
{
var data = new Channel<int>();
var quit = new Channel<bool>();
ThreadPool.QueueUserWorkItem(state => quit.Send(true));
var select = new Channels(Op.Recv(data), Op.Recv(quit));
var got = select.Select();
Assert.AreEqual(1, got.Index);
Assert.AreEqual(true, got.Value);
}
示例5: read_from_multiple_Channels
public void read_from_multiple_Channels()
{
var data = new Channel<int>();
var quit = new Channel<bool>();
ThreadPool.QueueUserWorkItem(state => data.Send(123));
var select = new Channels(Op.Recv(data), Op.Recv(quit));
var got = select.Select();
Assert.AreEqual(0, got.Index);
Assert.AreEqual(123, got.Value);
}
示例6: can_select_on_open_and_closed_Channels
public void can_select_on_open_and_closed_Channels()
{
var ch1 = new Channel<int>();
var ch2 = new Channel<bool>();
ThreadPool.QueueUserWorkItem(state => {
ch1.Close();
ch2.Send(true);
});
using (var select = new Channels(Op.Recv(ch1), Op.Recv(ch2))) {
var gotTrue = false;
while (!gotTrue) {
var got = select.Select();
if (got.Index == 1) {
Assert.AreEqual(true, got.Value);
Assert.AreEqual(false, gotTrue);
gotTrue = true;
} else {
Assert.AreEqual(0, got.Index, "select.Index");
Assert.AreEqual(null, got.Value, "select.Value");
}
}
}
}
示例7: can_select_on_Channels_closed_by_another_thread
public void can_select_on_Channels_closed_by_another_thread()
{
var ch1 = new Channel<int>();
var ch2 = new Channel<bool>();
ThreadPool.QueueUserWorkItem(state => {
ch1.Close();
ch2.Close();
});
using (var select = new Channels(Op.Recv(ch1), Op.Recv(ch2))) {
var got = select.Select();
Assert.AreNotEqual(-1, got.Index, "expected any channel to return");
Assert.AreEqual(null, got.Value);
}
}
示例8: z_2select_on_two_channels
public void z_2select_on_two_channels()
{
Benchmark.Go("select on two channels", (int runs) => {
var ch1 = new Channel<int>(100);
var ch2 = new Channel<int>(100);
ThreadPool.QueueUserWorkItem((state) => {
for (int i = 0; i < runs; i++) {
ch1.Send(i);
}
});
ThreadPool.QueueUserWorkItem((state) => {
for (int i = 0; i < runs; i++) {
ch2.Send(i);
}
});
int sum = 0;
int count = 0;
using (var select = new Channels(Op.Recv(ch1), Op.Recv(ch2))) {
int max = runs*2;
for (int i = 0; i < max; i++) {
count++;
var got = select.Select();
if (got.Index < 0) {
Assert.IsTrue(got.Index >= 0);
}
var gotInt = (ISelected<int>)got;
Maybe<int> val = gotInt.Value;
if (val.IsNone) {
Assert.AreNotEqual(Maybe<int>.None(), got.Value);
}
sum += val.Value;
}
}
});
}
示例9: z1_send_and_select_many_items_from_queued_channel
public void z1_send_and_select_many_items_from_queued_channel()
{
Benchmark.Go("select on buffer of 1", (int runs) => {
var data = new Channel<int>(10);
ThreadPool.QueueUserWorkItem((state) => {
for (int i = 0; i < runs; i++) {
data.Send(i);
}
});
using (var select = new Channels(Op.Recv(data))) {
for (int i = 0; i < runs; i++) {
ISelected got = select.Select();
if (got.Index != 0) {
Assert.AreEqual(0, got.Index);
}
var gotInt = (ISelected<int>)got;
if (gotInt.Value.Value != i) {
Assert.AreEqual(Maybe<int>.Some(i), got.Value);
}
}
}
});
}
示例10: select_on_one_null_channel
public void select_on_one_null_channel()
{
var ch1 = new Channel<int>();
var ch2 = new Channel<bool>(2);
ch2.Send(true);
using (var select = new Channels(Op.Recv(ch1), Op.Recv(ch2))) {
select.ClearAt(0);
var got = select.Select();
Assert.AreEqual(1, got.Index, "expected any channel to return");
Assert.AreEqual(true, got.Value);
}
}
示例11: select_on_closed_channel_does_not_block_and_returns_no_value
public void select_on_closed_channel_does_not_block_and_returns_no_value()
{
var ch1 = new Channel<int>(1);
ch1.Close();
using (var select = new Channels(Op.Recv(ch1))) {
var got = select.Select();
Assert.AreEqual(0, got.Index);
Assert.AreEqual(null, got.Value);
}
}
示例12: select_can_send_on_unbuffered_channel
public void select_can_send_on_unbuffered_channel()
{
var ch1 = new Channel<int>();
var done = new AutoResetEvent(false);
Maybe<int> recvd = Maybe<int>.None();
ThreadPool.QueueUserWorkItem(state => {
recvd = ch1.Recv();
done.Set();
});
using (var chans = new Channels(Op.Send(ch1))) {
chans[0].Value = 123;
var got = chans.Select();
Assert.AreEqual(0, got.Index, "select.Index");
}
done.WaitOne();
Assert.AreEqual(Maybe<int>.Some(123), recvd);
}
示例13: select_can_send_on_buffered_channel
public void select_can_send_on_buffered_channel()
{
var ch1 = new Channel<int>(1);
using (var chans = new Channels(Op.Send(ch1))) {
chans[0].Value = 123;
var got = chans.Select();
Assert.AreEqual(0, got.Index, "select.Index");
}
Assert.AreEqual(Maybe<int>.Some(123), ch1.Recv());
}
示例14: select_can_receive_on_unbuffered_channel
public void select_can_receive_on_unbuffered_channel()
{
var ch1 = new Channel<int>();
ThreadPool.QueueUserWorkItem(state => ch1.Send(123));
using (var select = new Channels(Op.Recv(ch1))) {
var got = select.Select();
Assert.AreEqual(0, got.Index, "select.Index");
Assert.AreEqual(123, got.Value, "select.Value");
}
}
示例15: select_can_receive_on_buffered_channel
public void select_can_receive_on_buffered_channel()
{
var ch1 = new Channel<int>(1);
ch1.Send(123);
using (var select = new Channels(Op.Recv(ch1))) {
var got = select.Select();
Assert.AreEqual(0, got.Index, "select.Index");
Assert.AreEqual(123, got.Value, "select.Value");
}
}