Exemple #1
0
        private static void PassingState()
        {
            var scheduler = new NewThreadScheduler();
            var myName    = "Lee";

            //WRONG WAY
            //scheduler.Schedule(() => { Console.WriteLine($"{nameof(myName)} = {myName}"); });

            //CORRECT
            scheduler.Schedule(myName,
                               (_, state) =>
            {
                Console.WriteLine(state);
                return(Disposable.Empty);
            });
            myName = "John";

            //DO NOT DO THAT, shared list was modified
            var list = new List <int>();

            scheduler.Schedule(list,
                               (innerScheduler, state) =>
            {
                Console.WriteLine(state.Count);
                return(Disposable.Empty);
            });
            list.Add(1);
        }
 public void ScheduleAbsolute()
 {
     var s = new NewThreadScheduler ();
     bool flag = false;
     s.Schedule (s.Now.AddMilliseconds (200), () => flag = true);
     Thread.Sleep (200);
     Assert.IsTrue (flag, "#1");
     flag = false;
     var dis = s.Schedule (s.Now.AddMilliseconds (200), () => flag = true);
     dis.Dispose ();
     Thread.Sleep (200); // make sure to wait for timer
     Assert.IsFalse (flag, "#2");
 }
        public void ScheduleAbsolute()
        {
            var  s    = new NewThreadScheduler();
            bool flag = false;

            s.Schedule(s.Now.AddMilliseconds(200), () => flag = true);
            Thread.Sleep(200);
            Assert.IsTrue(flag, "#1");
            flag = false;
            var dis = s.Schedule(s.Now.AddMilliseconds(200), () => flag = true);

            dis.Dispose();
            Thread.Sleep(200);              // make sure to wait for timer
            Assert.IsFalse(flag, "#2");
        }
Exemple #4
0
        private static void FutureScheduling()
        {
            var scheduler = new NewThreadScheduler();
            var delay     = TimeSpan.FromSeconds(1);

            Console.WriteLine("Before schedule at {0:o}", DateTime.Now);
            scheduler.Schedule(delay,
                               () => Console.WriteLine("Inside schedule at {0:o}", DateTime.Now));
            Console.WriteLine("After schedule at  {0:o}", DateTime.Now);
        }
        public void ExampleNewThreadSecondTask()
        {
            IScheduler scheduler = new NewThreadScheduler();

            Console.WriteLine("Starting on thread :{0}",
                              Thread.CurrentThread.ManagedThreadId);
            scheduler.Schedule("A", OuterAction);
            scheduler.Schedule("B", OuterAction);

            //Starting on thread :9
            //A start. ThreadId:10
            //A end. ThreadId:10
            //A.inner start . ThreadId:10
            //A.inner end. ThreadId:10
            //A.inner.Leaf. ThreadId:10
            //B start. ThreadId:11
            //B end. ThreadId:11
            //B.inner start . ThreadId:11
            //B.inner end. ThreadId:11
            //B.inner.Leaf. ThreadId:11
        }
Exemple #6
0
        private static void Cancellation()
        {
            var scheduler = new NewThreadScheduler();
            var delay     = TimeSpan.FromSeconds(1);

            Console.WriteLine("Before schedule at {0:o}", DateTime.Now);
            var token = scheduler.Schedule(delay,
                                           () => Console.WriteLine("Inside schedule at {0:o}", DateTime.Now));

            Console.WriteLine("After schedule at  {0:o}", DateTime.Now);
            token.Dispose();//remove from queue of work

            //cancel sth is running
            var list = new List <int>();

            Console.WriteLine("Enter to quit");
            var token2 = scheduler.Schedule(list, Work);

            Console.WriteLine("Press enter to cancel");
            Console.ReadLine();
            Console.WriteLine("cancelling...");
            token2.Dispose();
            Console.WriteLine("Cancelled");
        }