ThreadedBindingList.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ips.Library.Basic
  8. {
  9. public class ThreadedBindingList<T> : BindingList<T>
  10. {
  11. public ThreadedBindingList()
  12. {
  13. _ctx = SynchronizationContext.Current;
  14. }
  15. public SynchronizationContext SynchronizationContext
  16. {
  17. get { return _ctx; }
  18. set { _ctx = value; }
  19. }
  20. SynchronizationContext _ctx;
  21. protected override void OnAddingNew(AddingNewEventArgs e)
  22. {
  23. if (_ctx == null)
  24. {
  25. BaseAddingNew(e);
  26. }
  27. else
  28. {
  29. SynchronizationContext.Current.Send(delegate
  30. {
  31. BaseAddingNew(e);
  32. }, null);
  33. }
  34. }
  35. void BaseAddingNew(AddingNewEventArgs e)
  36. {
  37. base.OnAddingNew(e);
  38. }
  39. protected override void OnListChanged(ListChangedEventArgs e)
  40. {
  41. if (_ctx == null)
  42. {
  43. BaseListChanged(e);
  44. }
  45. else
  46. {
  47. _ctx.Send(delegate { BaseListChanged(e); }, null);
  48. }
  49. }
  50. void BaseListChanged(ListChangedEventArgs e)
  51. {
  52. base.OnListChanged(e);
  53. }
  54. }
  55. }