Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Task_Result_Property
- {
- class Account
- {
- public double Balance { get; set; }
- public Account(double sum)
- {
- Balance = sum;
- }
- public int CalcCreaditPoint()
- {
- Thread.Sleep(400);
- return (int)Balance / 10;
- }
- public double CalcUBalance(double currenty)
- {
- Thread.Sleep(400);
- return currenty * Balance;
- }
- }
- class Program
- {
- public static List<Account> accounts = new List<Account>();
- static void Main(string[] args)
- {
- Task createingAccountTask = new Task(CreateAccounts);
- Task<double> CalculateTotalTask = new Task<double>(CalculateAccountsTotal);
- createingAccountTask.Start();
- createingAccountTask.Wait();
- CalculateTotalTask.Start();
- CalculateTotalTask.Wait();
- double result = CalculateTotalTask.Result;
- Console.WriteLine("The result is: " + result);
- Console.ReadLine();
- }
- private static double CalculateAccountsTotal()
- {
- double total = 0;
- foreach (Account account in accounts)
- {
- Thread.Sleep(500);
- total += account.Balance;
- }
- return total;
- }
- private static void CreateAccounts()
- {
- double[] sums = { 12000, 8000, 10000, -400, 5400, 8000, 2240 };
- foreach (double sum in sums)
- {
- accounts.Add(new Account(sum));
- Thread.Sleep(200);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement