Seal_of_approval

task1(c)

May 15th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace task
  6. {
  7.     public class Service <T>
  8.     {
  9.         private List<T> list;
  10.  
  11.         private Function comparator;
  12.  
  13.         private event EventHandler end_Sort;
  14.         public delegate void EventHandler (List <T> list);
  15.  
  16.         public delegate int Function (T a, T b);
  17.  
  18.         public Service (List <T> list, Function comparator)
  19.         {
  20.             this.list = list;
  21.             this.comparator = comparator;
  22.         }
  23.  
  24.         public void Runner ()
  25.         {
  26.             Thread th1 = new Thread (delegate() {
  27.                 list.Sort (new Comparison <T> (comparator));
  28.                 if (end_Sort != null)
  29.                     end_Sort (list);
  30.             });
  31.             th1.Start ();
  32.         }
  33.  
  34.         public void AddHandler (EventHandler handler)
  35.         {
  36.             end_Sort += handler;
  37.         }
  38.     }
  39.  
  40.     class Program
  41.     {
  42.         public static int IntComparator(int a, int b)
  43.         {
  44.             return a.CompareTo(b);
  45.         }
  46.  
  47.         public static void Print<T>(List<T> list)
  48.         {
  49.             foreach (var item in list)
  50.                 Console.Write("{0} ", item);
  51.         }
  52.  
  53.         public static void Main(string[] args)
  54.         {
  55.             List<int> list = new List<int> { 9, 2, 4, 3, 8, 6, 3, 5 };
  56.             List<int> array = new List<int> { 9, 8, 7, 6, 5, 4, 3, 2 };
  57.             Service<int> intServ = new Service<int> (list, IntComparator);
  58.             intServ.AddHandler (Print<int>);
  59.             intServ.Runner ();
  60.  
  61.             Thread.Sleep (300);
  62.  
  63.             Console.WriteLine ();
  64.             Service<int> intServ_a = new Service<int> (array, IntComparator);
  65.             intServ_a.AddHandler (Print<int>);
  66.             intServ_a.Runner ();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment