Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. public class LotCache
  2. {
  3.  
  4. internal BackgroundWorker _fillPlantDataTable;
  5. private readonly DataGridView _frm;
  6. private Timer _gridRefresh;
  7. private BindingSource _source;
  8.  
  9.  
  10. /// <summary>
  11. /// Gets or sets the lots dictionary, which holds the lots for the forms usage
  12. /// </summary>
  13. /// <value>
  14. /// The lots dictionary.
  15. /// </value>
  16. public Dictionary<int, Lot> LotsDictionary { get; set; }
  17.  
  18. /// <summary>
  19. /// The display array used by the Blood Pits data grid
  20. /// </summary>
  21. public BindingList<LotCacheItem> DisplayArray;
  22.  
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="LotCache"/> class.
  25. /// </summary>
  26. /// <param name="dataGridView">The DataGridView that needs to be updated.</param>
  27. public LotCache(ref DataGridView dataGridView)
  28. {
  29. _frm = dataGridView;
  30. DisplayArray = new BindingList<LotCacheItem>();
  31. DisplayArray.AllowNew = true;
  32. DisplayArray.AllowRemove = true;
  33. DisplayArray.RaiseListChangedEvents = true;
  34.  
  35. DisplayArray.ListChanged += DisplayListUpdated;
  36. _source = new BindingSource(DisplayArray, null);
  37.  
  38. _fillPlantDataTable = new BackgroundWorker();
  39. _fillPlantDataTable.DoWork += FillDataTable;
  40.  
  41. FillLocalDataTable();
  42.  
  43.  
  44. }
  45. private void FillLocalDataTable()
  46. {
  47. Get_ByHarvestDateLocal();
  48.  
  49. }
  50. private void Get_ByHarvestDateLocal()
  51. {
  52. Lots _ls = new Lots();
  53.  
  54. try
  55. {
  56.  
  57. //This call gets lots from a local database
  58. DataTable _localLots = DataHelper.ExecuteProc("Local_LotCacheSelect", new ProcParams("HarvestDate", DateTime.Now));
  59. if (_localLots.Rows.Count > 0)
  60. DisplayArray.Clear();
  61. foreach (DataRow Row in _localLots.Rows)
  62. {
  63. Lot L = FillLots(Row);
  64. if (!_ls.ContainsKey(L.Lot_No))
  65. _ls.Add(Row.Field<short>("Lot_No"), L);
  66. }
  67.  
  68. LotsDictionary = _ls;
  69. StartGridRefresh();
  70. }
  71. catch (SqlException e)
  72. {
  73. MessageWindow.Show("Network Error", e.Message);
  74. }
  75. }
  76.  
  77. /// <summary>
  78. /// Get all Lots by harvest date.
  79. /// </summary>
  80. /// <param name="HarvestDate">The harvest date.</param>
  81. /// <returns>A <see cref="Lot" /></returns>
  82. private void StartGridRefresh()
  83. {
  84. int _refresh = 20;
  85. _gridRefresh = new Timer { Interval = _refresh * 1000 };
  86. _gridRefresh.Tick += GridRefresh_Tick;
  87. _gridRefresh.Enabled = true;
  88. }
  89.  
  90. private void GridRefresh_Tick(object sender, EventArgs e)
  91. {
  92. if (!_fillPlantDataTable.IsBusy) //<------------This is always busy after first execution
  93. _fillPlantDataTable.RunWorkerAsync();
  94. }
  95.  
  96. private void FillDataTable(object sender, DoWorkEventArgs e)
  97. {
  98. Debug.WriteLine("Im in fillDataTable: " + Thread.CurrentThread.ManagedThreadId + " " + DateTime.Now);
  99. try
  100. {
  101.  
  102.  
  103. DataTable _lotCacheTable = DataHelper.ExecuteProc("Local_LotCacheSelect", new ProcParams("HarvestDate", DateTime.Now));
  104.  
  105.  
  106. DisplayArray.RaiseListChangedEvents = false;
  107. Debug.WriteLine("Clear: " + Thread.CurrentThread.ManagedThreadId + " " + DateTime.Now);
  108. DisplayArray.Clear();
  109. Debug.WriteLine("End Clear: " + Thread.CurrentThread.ManagedThreadId + " " + DateTime.Now);
  110. foreach (DataRow Row in _lotCacheTable.Rows)
  111. {
  112. LotCacheItem _lc = new LotCacheItem();
  113. _lc.Lot_No = Row.Field<short>("Lot_No");
  114. _lc.HeadCount = Row.Field<int>("HeadCount");
  115. _lc.ProducerName = Row.Field<string>("Producername");
  116. _lc.BloodPittedCount = 0;
  117.  
  118. DisplayArray.Add(_lc);
  119. }
  120.  
  121. Debug.WriteLine("Grid Filled: " + Thread.CurrentThread.ManagedThreadId + " " + DateTime.Now);
  122. DisplayArray.RaiseListChangedEvents = true;
  123.  
  124. DisplayArray.ResetBindings(); //<--Never Finishes
  125. Debug.WriteLine("Bindings Reset: " + Thread.CurrentThread.ManagedThreadId + " " + DateTime.Now);
  126.  
  127. }
  128. catch (Exception _e)
  129. {
  130. //MessageWindow.Show("", _e.Message);
  131. }
  132. }
  133. }
  134.  
  135. public class LotCacheItem
  136. {
  137. public int Lot_No { get; set; }
  138. public DateTime HarvestDate { get; set; }
  139. public int HeadCount { get; set; }
  140. public string ProducerName { get; set; }
  141.  
  142. public string Age { get; set; }
  143.  
  144. public string Cool { get; set; }
  145.  
  146. public string CustomHarvest { get; set; }
  147.  
  148. public int BloodPittedCount { get; set; }
  149.  
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement