コード例 #1
0
        public void AsyncWaitTest()
        {
            EventClass e1        = new EventClass();
            EventClass e2        = new EventClass();
            EventClass e3        = new EventClass();
            EventClass syncClass = new EventClass();

            EventClass[] events = new EventClass[4] {
                e1, e2, e3, syncClass
            };

            List <EventClass> completedEvents = new List <EventClass>();

            using (EventExecutor executor = new EventExecutor())
            {
                executor.Start();
                executor.AddEvent(
                    async delegate()
                {
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                    await e1.AsyncWaitAndExecute(
                        2 * 1000,
                        delegate()
                    {
                        lock ( completedEvents )
                        {
                            completedEvents.Add(e1);
                        }
                    }
                        );
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                }
                    );

                executor.AddEvent(
                    async delegate()
                {
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                    await e2.AsyncWaitAndExecute(
                        3 * 1000,
                        delegate()
                    {
                        lock ( completedEvents )
                        {
                            completedEvents.Add(e2);
                        }
                    }
                        );
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                }
                    );


                executor.AddEvent(
                    async delegate()
                {
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                    await e3.AsyncWaitAndExecute(
                        4 * 1000,
                        delegate()
                    {
                        lock ( completedEvents )
                        {
                            completedEvents.Add(e3);
                        }
                    }
                        );
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                }
                    );

                executor.AddEvent(
                    delegate()
                {
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                    syncClass.WaitAndExecute(
                        10 * 1000,
                        delegate()
                    {
                        lock ( completedEvents )
                        {
                            completedEvents.Add(syncClass);
                        }
                    }
                        );
                    Assert.AreEqual(EventExecutor.DefaultThreadName, Thread.CurrentThread.Name);
                }
                    );

                foreach (EventClass ec in events)
                {
                    Assert.IsTrue(ec.Join(30 * 1000));
                    Assert.IsTrue(ec.Executed);
                }
            }

            Assert.AreEqual(4, completedEvents.Count);
        }