public static void ChapterMain()
        {
            // Use Task.Factory.StartNew<string>() for
            // TPL prior to .NET 4.5
            Task <string> task =
                Task.Run <string>(
                    () => PiCalculator.Calculate(100));

            foreach (
                char busySymbol in Utility.BusySymbols())
            {
                if (task.IsCompleted)
                {
                    Console.Write('\b');
                    break;
                }
                Console.Write(busySymbol);
            }

            Console.WriteLine();

            Console.WriteLine(task.Result);
            if (!task.IsCompleted)
            {
                throw new Exception("Task Should Be Completed");
            }
        }
        public static void Main()
        {
            // Use Task.Factory.StartNew<string>() for
            // TPL prior to .NET 4.5
            Task <string> task =
                Task.Run <string>(
                    () => PiCalculator.Calculate(100));

            foreach (
                char busySymbol in Utility.BusySymbols())
            {
                if (task.IsCompleted)
                {
                    Console.Write('\b');
                    break;
                }
                Console.Write(busySymbol);
            }

            Console.WriteLine();

            Console.WriteLine(task.Result);
            System.Diagnostics.Trace.Assert(
                task.IsCompleted);
        }
Example #3
0
        public static void ChapterMain()
        {
            Task <string> task = Task.Factory.StartNew <string>(
                () => PiCalculator.Calculate(10));

            Task faultedTask = task.ContinueWith(
                (antecedentTask) =>
            {
                Trace.Assert(task.IsFaulted);
                Console.WriteLine("Task State: Faulted");
            },
                TaskContinuationOptions.OnlyOnFaulted);
            Task canceledTask = task.ContinueWith(
                (antecedentTask) =>
            {
                Trace.Assert(task.IsCanceled);
                Console.WriteLine("Task State: Canceled");
            },
                TaskContinuationOptions.OnlyOnCanceled);
            Task completedTask = task.ContinueWith(
                (antecedentTask) =>
            {
                Trace.Assert(task.IsCompleted);
                Console.WriteLine("Task State: Completed");
            },
                TaskContinuationOptions.OnlyOnRanToCompletion);

            completedTask.Wait();
        }
Example #4
0
        private void CalculatePi(IAsyncAction operation)
        {
            if (isStopped)
            {
                return;
            }

            string result = PiCalculator.Calculate(PI_DIGITS);

            Debug.WriteLine(result);
        }
Example #5
0
        public static void ChapterMain()
        {
            string    pi         = null;
            const int iterations = TotalDigits / BatchSize;

            for (int i = 0; i < iterations; i++)
            {
                pi += PiCalculator.Calculate(
                    BatchSize, i * BatchSize);
            }

            Console.WriteLine(pi);
        }
Example #6
0
        public static void Main()
        {
            string    pi         = null;
            const int iterations = TotalDigits / BatchSize;

            string[] sections = new string[iterations];
            //TPL提供了一个辅助方法,并行迭代
            Parallel.For(0, iterations, (i) =>
            {
                sections[i] = PiCalculator.Calculate(BatchSize, i * BatchSize);
            });
            pi = string.Join("", sections);
            Console.WriteLine(pi);
        }
Example #7
0
        public static void ChapterMain()
        {
            string pi         = null;
            int    iterations = TotalDigits / BatchSize;

            string[] sections = new string[iterations];
            Parallel.For(0, iterations, (i) =>
            {
                sections[i] += PiCalculator.Calculate(
                    BatchSize, i * BatchSize);
            });
            pi = string.Join("", sections);
            Console.WriteLine(pi);
        }
        private static void WritePi(
            CancellationToken cancellationToken)
        {
            const int batchSize = 1;
            string    piSection = string.Empty;
            int       i         = 0;

            while (!cancellationToken.IsCancellationRequested ||
                   i == int.MaxValue)
            {
                piSection = PiCalculator.Calculate(
                    batchSize, (i++) * batchSize);
                Console.Write(piSection);
            }
        }
        public static void ChapterMain()
        {
            // Use Task.Factory.StartNew<string>() for
            // TPL prior to .NET 4.5
            Task <string> task =
                Task.Run <string>(
                    () => PiCalculator.Calculate(10));
            Task faultedTask = task.ContinueWith(
                (antecedentTask) =>
            {
                if (!antecedentTask.IsFaulted)
                {
                    throw new Exception("Antecedent Task Should Be Faulted");
                }
                Console.WriteLine(
                    "Task State: Faulted");
            },
                TaskContinuationOptions.OnlyOnFaulted);

            Task canceledTask = task.ContinueWith(
                (antecedentTask) =>
            {
                if (!antecedentTask.IsCanceled)
                {
                    throw new Exception("Antecedent Task Should Be Canceled");
                }
                Console.WriteLine(
                    "Task State: Canceled");
            },
                TaskContinuationOptions.OnlyOnCanceled);

            Task completedTask = task.ContinueWith(
                (antecedentTask) =>
            {
                if (!antecedentTask.IsCompleted)
                {
                    throw new Exception("Antecedent Task Should Be Completed");
                }
                Console.WriteLine(
                    "Task State: Completed");
            }, TaskContinuationOptions.
                OnlyOnRanToCompletion);

            completedTask.Wait();
        }
Example #10
0
 public void CalculateAsync <TState>(
     int digits,
     CancellationToken cancelToken,
     TState userState)
 {
     Task <string> .Factory.StartNew(
         () => PiCalculator.Calculate(digits), cancelToken)
     .ContinueWith <string>(
         continueTask =>
     {
         CalculateCompleted(typeof(PiCalculator),
                            new CalculateCompletedEventArgs(
                                continueTask.Result,
                                continueTask.Exception,
                                cancelToken.IsCancellationRequested,
                                userState));
         return(continueTask.Result);
     });
 }
        public void CalculateAsync <TState>(
            int digits,
            CancellationToken cancelToken
            = default(CancellationToken),
            TState userState
            = default(TState))
        {
            SynchronizationContext.
            SetSynchronizationContext(
                AsyncOperationManager.
                SynchronizationContext);

            // Ensure the continuation runs on the current
            // thread, and that therefore the event will
            // be raised on the same thread that called
            // this method in the first place.
            TaskScheduler scheduler =
                TaskScheduler.
                FromCurrentSynchronizationContext();

            Task.Run(
                () =>
            {
                return(PiCalculator.Calculate(digits));
            }, cancelToken)
            .ContinueWith(
                continueTask =>
            {
                Exception exception =
                    continueTask.Exception == null ?
                    continueTask.Exception :
                    continueTask.Exception.
                    InnerException;
                CalculateCompleted(
                    typeof(PiCalculator),
                    new CalculateCompletedEventArgs(
                        continueTask.Result,
                        exception,
                        cancelToken.IsCancellationRequested,
                        userState));
            }, scheduler);
        }
Example #12
0
        static void Main(string[] args)
        {
            Task <string> task = Task.Run <string>(() => PiCalculator.Calculate(100000));

            foreach (var busySymbol in Utility.BusySymbols())
            {
                if (task.IsCompleted)
                {
                    Console.WriteLine('\b');
                    break;
                }
                Console.WriteLine(busySymbol);//太快的话,这会被忽略
            }

            Console.WriteLine();

            Console.WriteLine(task.Result);                    //会阻塞调用任务室外线程

            System.Diagnostics.Trace.Assert(task.IsCompleted); //判断task.IsCompleted真假,false则出现弹框
        }
        public static void Main()
        {
            Task <string> task = Task.Factory.StartNew <string>(
                () => PiCalculator.Calculate(100));

            foreach (char busySymbol in Utility.BusySymbols())
            {
                if (task.IsCompleted)
                {
                    Console.Write('\b');
                    break;
                }
                Console.Write(busySymbol);
            }

            Console.WriteLine();
            // Blocks until task completes.
            Console.WriteLine(task.Result);
            Trace.Assert(task.IsCompleted);
        }