Esempio n. 1
0
        static void Main(string[] args)
        {
            // create a new instance of the immutable type
            MyImmutableType immutable = new MyImmutableType();

            // create a cancellation token source
            CancellationTokenSource tokenSource = new CancellationTokenSource();

            // create a task that will calculate the circumference
            // of a 1 unit circle and check the result
            Task task1 = new Task(() => {
                while (true)
                {
                    // perform the calculation
                    double circ = 2 * immutable.refData.PI * immutable.circleSize;
                    Console.WriteLine("Circumference: {0}", circ);
                    // check for the mutation
                    if (circ == 4)
                    {
                        // the mutation has occurred - break
                        // out of the loop
                        Console.WriteLine("Mutation detected");
                        break;
                    }
                    // sleep for a moment
                    tokenSource.Token.WaitHandle.WaitOne(250);
                }
            }, tokenSource.Token);

            // start the task
            task1.Start();

            // wait to let the task start work
            Thread.Sleep(1000);

            // perform the mutation
            immutable.refData.PI = 2;

            // join the task
            task1.Wait();

            // wait for input before exiting
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // create a new instance of the immutable type
            MyImmutableType immutable = new MyImmutableType();

            // create a cancellation token source
            CancellationTokenSource tokenSource = new CancellationTokenSource();

            // create a task that will calculate the circumference
            // of a 1 unit circle and check the result
            Task task1 = new Task(() => {
                while (true) {
                    // perform the calculation
                    double circ = 2 * immutable.refData.PI * immutable.circleSize;
                    Console.WriteLine("Circumference: {0}", circ);
                    // check for the mutation
                    if (circ == 4) {
                        // the mutation has occurred - break
                        // out of the loop
                        Console.WriteLine("Mutation detected");
                        break;
                    }
                    // sleep for a moment
                    tokenSource.Token.WaitHandle.WaitOne(250);
                }
            }, tokenSource.Token);

            // start the task
            task1.Start();

            // wait to let the task start work
            Thread.Sleep(1000);

            // perform the mutation
            immutable.refData.PI = 2;

            // join the task
            task1.Wait();

            // wait for input before exiting
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }