Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1. Managed it by educating BindingList<T> about sync-context; a "keeper" I
  2. reckon ;-p
  3. Most of this code is demo; it is just the list that you need...
  4.  
  5. Marc
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.     public abstract class EntityBase : INotifyPropertyChanged {
  16.         public event PropertyChangedEventHandler PropertyChanged;
  17.  
  18.         protected virtual bool UpdateField<T>(ref T field, T value, string
  19. propertyName) {
  20.             if (EqualityComparer<T>.Default.Equals(field, value)) return
  21. false; // no change
  22.             field = value;
  23.             if (!string.IsNullOrEmpty(propertyName))
  24. OnPropertyChanged(propertyName);
  25.             return true;
  26.         }
  27.         protected virtual void OnPropertyChanged(string propertyName) {
  28.             PropertyChangedEventHandler handler =  PropertyChanged;
  29.             if (handler != null) {
  30.                 handler(this, new PropertyChangedEventArgs(propertyName));
  31.             }
  32.         }
  33.     }
  34.  
  35.     public class SomeEntity : EntityBase
  36.     {
  37.         public SomeEntity() { }
  38.         public SomeEntity(string forename, string surname, DateTime
  39. dateOfBirth)
  40.         {
  41.             Forename = forename;
  42.             Surname = surname;
  43.             DateOfBirth = dateOfBirth;
  44.         }
  45.         private string forename, surname;
  46.         private DateTime dateOfBirth;
  47.  
  48.         public string Forename {
  49.             get { return forename; }
  50.             set { UpdateField(ref forename, value, "Forename"); }
  51.         }
  52.         public string Surname {
  53.             get { return surname; }
  54.             set { UpdateField(ref surname, value, "Surname"); }
  55.         }
  56.         public DateTime DateOfBirth {
  57.             get { return dateOfBirth; }
  58.             set { UpdateField(ref dateOfBirth, value, "DateOfBirth"); }
  59.         }
  60.     }
  61.  
  62.     static class Program
  63.     {
  64.         [STAThread]
  65.         static void Main()
  66.         {
  67.             ThreadedBindingList<SomeEntity> data = new
  68. ThreadedBindingList<SomeEntity>();
  69.             data.Add(new SomeEntity());
  70.             data.Add(new SomeEntity());
  71.             data.Add(new SomeEntity());
  72.             Application.EnableVisualStyles();
  73.             using(Form f = new Form())
  74.             using (DataGridView dgv = new DataGridView())
  75.             {
  76.                 dgv.Dock = DockStyle.Fill;
  77.                 dgv.DataSource = data;
  78.                 f.Controls.Add(dgv);
  79.                 f.Load += delegate {
  80.  
  81.                     ThreadPool.QueueUserWorkItem(delegate
  82.                     {
  83.                         StartRandomEditing(data);
  84.                     });
  85.                 };
  86.                 Application.Run(f);
  87.             }
  88.         }
  89.         static void StartRandomEditing(IList<SomeEntity> data)
  90.         {
  91.             Random rand = new Random();
  92.             while (true)
  93.             {
  94.                 Thread.Sleep(1000);
  95.                 int index = rand.Next(data.Count);
  96.                 SomeEntity item = data[index];
  97.                 switch (rand.Next(3))
  98.                 {
  99.                     case 0:
  100.                         item.Forename += "#";
  101.                         break;
  102.                     case 1:
  103.                         item.Surname += "#";
  104.                         break;
  105.                     case 2:
  106.                         item.DateOfBirth += TimeSpan.FromDays(1);
  107.                         break;
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     public class ThreadedBindingList<T> : BindingList<T>
  114.     {
  115.         protected override void OnAddingNew(AddingNewEventArgs e)
  116.         {
  117.         SynchronizationContext ctx = SynchronizationContext.Current;
  118.         if (ctx == null)
  119.         {
  120.             BaseAddingNew(e);
  121.         }
  122.         else
  123.         {
  124.             SynchronizationContext.Current.Send(delegate
  125.             {
  126.                 BaseAddingNew(e);
  127.             }, null);
  128.         }
  129.         }
  130.         void BaseAddingNew(AddingNewEventArgs e)
  131.         {
  132.             base.OnAddingNew(e);
  133.         }
  134.         protected override void OnListChanged(ListChangedEventArgs e)
  135.         {
  136.             SynchronizationContext ctx = SynchronizationContext.Current;
  137.             if (ctx == null)
  138.             {
  139.                 BaseListChanged(e);
  140.             }
  141.             else
  142.             {
  143.                 SynchronizationContext.Current.Send(delegate
  144.                 {
  145.                     BaseListChanged(e);
  146.                 }, null);
  147.             }
  148.         }
  149.         void BaseListChanged(ListChangedEventArgs e)
  150.         {
  151.             base.OnListChanged(e);
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement