Advertisement
ivandrofly

Task ContinueWith - C#

Feb 26th, 2014
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Threading;
  5.  
  6. namespace Task_ContinueWith
  7. {
  8.     class Account
  9.     {
  10.         public double Balance { get; set; }
  11.         public Account(double sum)
  12.         {
  13.             Balance = sum;
  14.         }
  15.         public int CalcCreaditPoint()
  16.         {
  17.             Thread.Sleep(400);
  18.             return (int)Balance / 10;
  19.         }
  20.         public double CalcUBalance(double currenty)
  21.         {
  22.             Thread.Sleep(400);
  23.             return currenty * Balance;
  24.         }
  25.     }
  26.     class Program
  27.     {
  28.         public static List<Account> accounts = new List<Account>();
  29.         static void Main(string[] args)
  30.         {
  31.             Task creatingAccountsTask = new Task(CreateAccounts);
  32.             Task printTotalTask = creatingAccountsTask.ContinueWith((t) => PrintAccountTotal());
  33.             creatingAccountsTask.Start();
  34.             Console.ReadLine();
  35.         }
  36.  
  37.         private static void PrintAccountTotal()
  38.         {
  39.             double total = 0;
  40.             foreach (Account account in accounts)
  41.             {
  42.                 total += account.Balance;
  43.             }
  44.             Console.WriteLine(total);
  45.         }
  46.         public static void CreateAccounts()
  47.         {
  48.             double[] sums = { 12000, 8000, 10000, -400, 5400, 8000, 2240 };
  49.             foreach (double sum in sums)
  50.             {
  51.                 accounts.Add(new Account(sum));
  52.                 Thread.Sleep(200);
  53.             }
  54.         }
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement