Advertisement
athulrajts

Untitled

Aug 1st, 2020
2,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading;
  8.  
  9. namespace LoopingEnumerable
  10. {
  11.     class Program
  12.     {
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             Console.WriteLine("Starting Loop");
  17.  
  18.             var collection = new ObservableCollection<Translation>
  19.             {
  20.                 new Translation(),
  21.                 new Translation(),
  22.                 new Translation(),
  23.                 new Translation(),
  24.                 new Translation(),
  25.                 new Translation(),
  26.                 new Translation(),
  27.                 new Translation(),
  28.                 new Translation(),
  29.                 new Translation(),
  30.             };
  31.  
  32.             var source = collection.Where(x => string.IsNullOrEmpty(x.TranslatedText));
  33.  
  34.             var count = source.Count();
  35.  
  36.             Console.WriteLine($"Count : {count}");
  37.  
  38.             var token = new CancellationToken();
  39.  
  40.             var progress = new Progress<int>(i => Console.WriteLine(i));
  41.  
  42.             Console.WriteLine("Starting Loop");
  43.  
  44.             Loop_For(source, token, progress);
  45.  
  46.             //Loop_ForEach(source, token, progress);
  47.  
  48.             Console.WriteLine("Finished Loop");
  49.  
  50.             Console.ReadKey();
  51.         }
  52.  
  53.         public static void Loop_For(IEnumerable<Translation> items, CancellationToken cancellationToken, IProgress<int> progress)
  54.         {
  55.             for (int i = 0; i < items.Count(); i++)
  56.             {
  57.                 if (cancellationToken.IsCancellationRequested)
  58.                 {
  59.                     return;
  60.                 }
  61.  
  62.  
  63.                 var obj = items.ElementAt(i);
  64.  
  65.                 obj.TranslatedText = "something";
  66.  
  67.                 progress.Report(i + 1);
  68.  
  69.                 Thread.Sleep(200);
  70.             }
  71.         }
  72.  
  73.         public static void Loop_ForEach(IEnumerable<Translation> items, CancellationToken cancellationToken, IProgress<int> progress)
  74.         {
  75.             int count = 0;
  76.             foreach (var item in items)
  77.             {
  78.                 if (cancellationToken.IsCancellationRequested)
  79.                 {
  80.                     return;
  81.                 }
  82.  
  83.                 item.TranslatedText = "something";
  84.  
  85.                 progress.Report(++count);
  86.  
  87.                 Thread.Sleep(200);
  88.             }
  89.         }
  90.     }
  91.  
  92.     public class Translation : BindableBase
  93.     {
  94.         public string Key { get; set; }
  95.         public string EnglishText { get; set; } = "Hello World";
  96.  
  97.         private string translatedText;
  98.         public string TranslatedText
  99.         {
  100.             get { return translatedText; }
  101.             set { SetProperty(ref translatedText, value); }
  102.         }
  103.     }
  104.  
  105.     public class BindableBase : INotifyPropertyChanged
  106.     {
  107.         public event PropertyChangedEventHandler PropertyChanged;
  108.         protected void RaisePropertyChanged([CallerMemberName] string property = "")
  109.             => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
  110.  
  111.         protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string property = "")
  112.         {
  113.             if(EqualityComparer<T>.Default.Equals(storage, value) == false)
  114.             {
  115.                 storage = value;
  116.                 RaisePropertyChanged(property);
  117.  
  118.                 return true;
  119.             }
  120.  
  121.             return false;
  122.         }
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement