Advertisement
ivandrofly

Task Result - C#

Feb 26th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5.  
  6. namespace Task_Result_Property
  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 createingAccountTask = new Task(CreateAccounts);
  32.             Task<double> CalculateTotalTask = new Task<double>(CalculateAccountsTotal);
  33.             createingAccountTask.Start();
  34.             createingAccountTask.Wait();
  35.  
  36.             CalculateTotalTask.Start();
  37.             CalculateTotalTask.Wait();
  38.             double result = CalculateTotalTask.Result;
  39.             Console.WriteLine("The result is: " + result);
  40.             Console.ReadLine();
  41.         }
  42.  
  43.         private static double CalculateAccountsTotal()
  44.         {
  45.             double total = 0;
  46.             foreach (Account account in accounts)
  47.             {
  48.                 Thread.Sleep(500);
  49.                 total += account.Balance;
  50.             }
  51.             return total;
  52.         }
  53.  
  54.         private static void CreateAccounts()
  55.         {
  56.             double[] sums = { 12000, 8000, 10000, -400, 5400, 8000, 2240 };
  57.             foreach (double sum in sums)
  58.             {
  59.                 accounts.Add(new Account(sum));
  60.                 Thread.Sleep(200);
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement