Exemplo n.º 1
0
        static void Main(string[] args) {

            // create the bank account instance
            BankAccount account = new BankAccount();

            // create an array of tasks
            Task<int>[] tasks = new Task<int>[10];

            for (int i = 0; i < 10; i++) {
                // create a new task
                tasks[i] = new Task<int>((stateObject) => {

                    // get the state object
                    int isolatedBalance = (int)stateObject;

                    // enter a loop for 1000 balance updates
                    for (int j = 0; j < 1000; j++) {
                        // update the balance
                        isolatedBalance++;
                    }

                    // return the updated balance
                    return isolatedBalance;

                }, account.Balance);

                // start the new task
                tasks[i].Start();
            }

            // get the result from each task and add it to
            // the balance
            for (int i = 0; i < 10; i++) {
                account.Balance += tasks[i].Result;
            }

            // write out the counter value
            Console.WriteLine("Expected value {0}, Balance: {1}",
                10000, account.Balance);

            // wait for input before exiting
            Console.WriteLine("Press enter to finish");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Task<BankAccount> rootTask = new Task<BankAccount>(() => {
                // create a new bank account
                BankAccount account = new BankAccount();
                // enter a loop
                for (int i = 0; i < 1000; i++) {
                    // increment the account total
                    account.Balance++;
                }
                // return the bank account
                return account;
            });

            // create the second generation task, which will double the antecdent balance
            Task<int> continuationTask1
                = rootTask.ContinueWith<int>((Task<BankAccount> antecedent) => {
                    Console.WriteLine("Interim Balance 1: {0}", antecedent.Result.Balance);
                    return antecedent.Result.Balance * 2;
                });

            // create a third generation task, which will print out the result
            Task continuationTask2
                = continuationTask1.ContinueWith((Task<int> antecedent) => {
                    Console.WriteLine("Final Balance 1: {0}", antecedent.Result);
                });

            // create a second and third generation task in one step
            rootTask.ContinueWith<int>((Task<BankAccount> antecedent) => {
                Console.WriteLine("Interim Balance 2: {0}", antecedent.Result.Balance);
                return antecedent.Result.Balance / 2;
            }).ContinueWith((Task<int> antecedent) => {
                Console.WriteLine("Final Balance 2: {0}", antecedent.Result);
            });

            // start the task
            rootTask.Start();

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