Exemplo n.º 1
0
        //15 超时接收
        static async Task Worker15()
        {
            chan <int> chan = chan <int> .make(1);//缓存1个

            generator.children children = new generator.children();
            children.go(async delegate()
            {
                for (int i = 0; i < 3; i++)
                {
                    await chan.send(i);
                    await generator.sleep(1000);
                }
            });
            children.go(async delegate()
            {
                while (true)
                {
                    chan_recv_wrap <int> res = await chan.timed_receive(2000);
                    if (res.state == chan_state.overtime)
                    {
                        Log($"recv 超时");
                        break;
                    }
                    Log($"recv {res.msg}");
                }
            });
            await children.wait_all();
        }
Exemplo n.º 2
0
        //17 过程调用
        static async Task Worker17()
        {
            csp_chan <int, tuple <int, int> > csp = new csp_chan <int, tuple <int, int> >();

            generator.children children = new generator.children();
            children.go(async delegate()
            {
                for (int i = 0; i < 3; i++)
                {
                    csp_invoke_wrap <int> res = await csp.invoke(tuple.make(i, i));
                    Log($"csp 返回 {res.result}");
                    await generator.sleep(1000);
                }
                csp.close();
            });
            children.go(async delegate()
            {
                while (true)
                {
                    chan_state st = await generator.csp_wait(csp, async delegate(tuple <int, int> p)
                    {
                        Log($"recv {p}");
                        await generator.sleep(1000);
                        return(p.value1 + p.value2);
                    });
                    if (st == chan_state.closed)
                    {
                        Log($"csp 已关闭");
                        break;
                    }
                }
            });
            await children.wait_all();
        }
Exemplo n.º 3
0
        //16 超时发送
        static async Task Worker16()
        {
            chan <int> chan = chan <int> .make(0);//无缓存

            generator.children children = new generator.children();
            children.go(async delegate()
            {
                for (int i = 0; ; i++)
                {
                    chan_send_wrap res = await chan.timed_send(2000, i);
                    if (res.state == chan_state.overtime)
                    {
                        Log($"send 超时");
                        break;
                    }
                    await generator.sleep(1000);
                }
            });
            children.go(async delegate()
            {
                for (int i = 0; i < 3; i++)
                {
                    chan_recv_wrap <int> res = await chan.receive();
                    Log($"recv0 {res.msg}");
                }
            });
            await children.wait_all();
        }
Exemplo n.º 4
0
        //9 超时等待一组任务,然后中止所有任务,且在中止任务中就地善后处理
        static async Task Worker9()
        {
            generator.children children = new generator.children();
            children.go(() => Worker("A", 1000));
            children.go(async delegate()
            {
                try
                {
                    await Worker("B", 2000);
                }
                catch (generator.stop_exception)
                {
                    Log("B被中止");
                    await generator.sleep(500);
                    throw;
                }
                catch (System.Exception)
                {
                }
            });
            var task = await children.timed_wait_all(1500);

            await children.stop();

            Log($"成功{task.Count}个");
        }
Exemplo n.º 5
0
 //2 A、B、C全部并行,且依赖同一个strand(隐含参数,所有依赖同一个strand的任务都是线程安全的)
 //A
 //B
 //C
 static async Task Worker2()
 {
     generator.children children = new generator.children();
     children.go(() => Worker("A"));
     children.go(() => Worker("B"));
     children.go(() => Worker("C"));
     await children.wait_all();
 }
Exemplo n.º 6
0
        //8 超时等待一组任务,然后中止所有任务
        static async Task Worker8()
        {
            generator.children children = new generator.children();
            children.go(() => Worker("A", 1000));
            children.go(() => Worker("B", 2000));
            var tasks = await children.timed_wait_all(1500);

            await children.stop();

            Log($"成功{tasks.Count}个");
        }
Exemplo n.º 7
0
        //19 一轮选择一个发送
        static async Task Worker19()
        {
            chan <int> chan1 = chan <int> .make(0);

            chan <int> chan2 = chan <int> .make(0);

            generator.children children = new generator.children();
            children.go(async delegate()
            {
                for (int i = 0; i < 3; i++)
                {
                    await generator.select(true).case_send(chan1, i, async delegate()
                    {
                        Log($"send1 {i}");
                        await generator.sleep(1000);
                    }).case_send(chan2, i, async delegate()
                    {
                        Log($"send2 {i}");
                        await generator.sleep(1000);
                    }).end();
                }
                chan1.close();
                chan2.close();
            });
            children.go(async delegate()
            {
                while (true)
                {
                    chan_recv_wrap <int> res = await chan1.receive();
                    if (res.state == chan_state.closed)
                    {
                        Log($"chan1 已关闭");
                        break;
                    }
                    Log($"recv1 {res.msg}");
                }
            });
            children.go(async delegate()
            {
                while (true)
                {
                    chan_recv_wrap <int> res = await chan2.receive();
                    if (res.state == chan_state.closed)
                    {
                        Log($"chan2 已关闭");
                        break;
                    }
                    Log($"recv2 {res.msg}");
                }
            });
            await children.wait_all();
        }
Exemplo n.º 8
0
        //14 生产者->消费者
        static async Task Worker14()
        {
            chan <int> chan = chan <int> .make(-1);//无限缓存,使用默认strand

            generator.children children = new generator.children();
            children.go(async delegate()
            {
                for (int i = 0; i < 3; i++)
                {
                    await chan.send(i);
                    await generator.sleep(1000);
                }
                chan.close();
            });
            children.go(async delegate()
            {
                for (int i = 10; i < 13; i++)
                {
                    await chan.send(i);
                    await generator.sleep(1000);
                }
                chan.close();
            });
            children.go(async delegate()
            {
                while (true)
                {
                    chan_recv_wrap <int> res = await chan.receive();
                    if (res.state == chan_state.closed)
                    {
                        Log($"chan 已关闭");
                        break;
                    }
                    Log($"recv0 {res.msg}");
                }
            });
            children.go(async delegate()
            {
                while (true)
                {
                    chan_recv_wrap <int> res = await chan.receive();
                    if (res.state == chan_state.closed)
                    {
                        Log($"chan 已关闭");
                        break;
                    }
                    Log($"recv1 {res.msg}");
                }
            });
            await children.wait_all();
        }
Exemplo n.º 9
0
 //10 嵌套任务
 static async Task Worker10()
 {
     generator.children children = new generator.children();
     children.go(async delegate()
     {
         generator.children children1 = new generator.children();
         children1.go(() => Worker("A"));
         children1.go(() => Worker("B"));
         await children1.wait_all();
     });
     children.go(async delegate()
     {
         generator.children children1 = new generator.children();
         children1.go(() => Worker("C"));
         children1.go(() => Worker("D"));
         await children1.wait_all();
     });
     await children.wait_all();
 }
Exemplo n.º 10
0
        //18 一轮选择一个接收
        static async Task Worker18()
        {
            chan <int> chan1 = chan <int> .make(0);

            chan <int> chan2 = chan <int> .make(0);

            generator.children children = new generator.children();
            children.go(async delegate()
            {
                for (int i = 0; i < 3; i++)
                {
                    await chan1.send(i);
                    await generator.sleep(1000);
                }
                chan1.close();
            });
            children.go(async delegate()
            {
                for (int i = 10; i < 13; i++)
                {
                    await chan2.send(i);
                    await generator.sleep(1000);
                }
                chan2.close();
            });
            children.go(async delegate()
            {
                await generator.select().case_receive(chan1, async delegate(int p)
                {
                    await generator.sleep(100);
                    Log($"recv1 {p}");
                }).case_receive(chan2, async delegate(int p)
                {
                    await generator.sleep(100);
                    Log($"recv2 {p}");
                }).loop();
                Log($"chan 已关闭");
            });
            await children.wait_all();
        }
Exemplo n.º 11
0
        //11 嵌套中止
        static async Task Worker11()
        {
            generator.children children = new generator.children();
            children.go(() => Worker("A", 1000));
            children.go(async delegate()
            {
                try
                {
                    generator.children children1 = new generator.children();
                    children1.go(async delegate()
                    {
                        try
                        {
                            await Worker("B", 2000);
                        }
                        catch (generator.stop_exception)
                        {
                            Log("B被中止1");
                            await generator.sleep(500);
                            throw;
                        }
                        catch (System.Exception)
                        {
                        }
                    });
                    await children1.wait_all();
                }
                catch (generator.stop_exception)
                {
                    Log("B被中止2");
                    throw;
                }
                catch (System.Exception)
                {
                }
            });
            await generator.sleep(1500);

            await children.stop();
        }
Exemplo n.º 12
0
        private async Task Task1Action(int time)
        {
            WaitForm waitAction = new WaitForm();

            generator.children children  = new generator.children();
            generator.child    taskChild = children.tgo(async delegate()
            {
                bool cancelTask = false;
                Task task       = Task.Run(delegate()
                {
                    int ct = time / 100;
                    for (int i = 0; i < ct && !cancelTask; i++)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                });
                try
                {
                    await generator.wait_task(task);
                }
                catch (generator.stop_exception)
                {
                    cancelTask = true;
                    await generator.wait_task(task);
                }
            });
            children.go(async delegate()
            {
                await generator.sleep(500);
                await generator.send_control(this, delegate()
                {
                    waitAction.StartPosition = FormStartPosition.CenterParent;
                    waitAction.ShowDialog();
                });
                if (waitAction.isCancel)
                {
                    taskChild.stop();
                }
            });
            await children.wait(taskChild);

            waitAction.Close();
            await children.stop();
        }