SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
ValuesUpdater.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Threading;
5 using System.ComponentModel;
6 using Seci.Definitions;
7 
8 namespace Seci.Tools
9 {
16  class ValuesUpdater : IDisposable
17  {
18  private BackgroundWorker _blockUpdater;
19  private BackgroundWorker _daeUpdater;
20  private Timer _updateTimer;
21  private TimerCallback _updateCallBack;
22  private int _updateRate = 500;
23  private static BlockInfo _statusBlock; //This is for logging the current run status.
24  private Boolean _daeUpdating = false;
25  private Boolean _timedOut = false;
26 
30  public delegate void ValuesUpdatedEventHandler(Boolean timeout);
31  public event ValuesUpdatedEventHandler OnValuesUpdated;
32 
37  public ValuesUpdater(int updateRate)
38  {
39  if (updateRate <= 0)
40  {
41  _updateRate = 500;
42  }
43  else
44  {
45  _updateRate = updateRate;
46  }
47 
48  createStatusBlock();
49 
50  if (_blockUpdater == null)
51  {
52  //Initialise the block background worker
53  _blockUpdater = new BackgroundWorker();
54  _blockUpdater.DoWork += new DoWorkEventHandler(blockUpdater_DoWork);
55  _blockUpdater.RunWorkerCompleted += new RunWorkerCompletedEventHandler(blockUpdater_RunWorkerCompleted);
56  }
57 
58  if (_daeUpdater == null)
59  {
60  //Initialise the dae background worker
61  _daeUpdater = new BackgroundWorker();
62  _daeUpdater.DoWork += new DoWorkEventHandler(_daeUpdater_DoWork);
63  _daeUpdater.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_daeUpdater_RunWorkerCompleted);
64  }
65  }
66 
67  private void createStatusBlock()
68  {
69  _statusBlock = new BlockInfo();
70  _statusBlock.BlockName = Status.StatusBlockName;
71  _statusBlock.LoggingRate = 5;
72  _statusBlock.LogChangesOnly = true;
73  _statusBlock.Tolerance = 0;
74  _statusBlock.LogValueToFile = true;
75  _statusBlock.BlockEnabled = true;
76  }
77 
81  public void StartUpdater()
82  {
83  if (_updateCallBack == null && _updateTimer == null)
84  {
85  _updateCallBack = new TimerCallback(updateTimer_Tick);
86  _updateTimer = new Timer(_updateCallBack, null, 0, _updateRate);
87  }
88  }
89 
93  public void PauseUpdater()
94  {
95  if (_updateTimer != null)
96  {
97  _updateTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
98  }
99  }
100 
104  public void ResumeUpdater()
105  {
106  if (_updateTimer != null)
107  {
108  _updateTimer.Change(0, _updateRate);
109  }
110  }
111 
117  private void updateTimer_Tick(object obj)
118  {
119  //Only update if not already running background thread
120  try
121  {
122  if (!_blockUpdater.IsBusy)
123  {
124  _blockUpdater.RunWorkerAsync();
125  }
126  if (!_daeUpdater.IsBusy)
127  {
128  _daeUpdater.RunWorkerAsync();
129  }
130  }
131  catch
132  {
133  }
134  }
135 
141  private void blockUpdater_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
142  {
143  try
144  {
146  {
147  Standard.DaeMonitor.GetRunControlSettings();
148  }
149 
150  //Get block values for config
151  Managers.BlockMgr.UpdateBlockValues();
152 
153  //Update block values on DAE monitor
155  {
156  //Standard.DaeMonitor.UpdateBlockValues();
157  }
158 
159  //Wait for the dae thread to catch up
160  //as we want the GUI to update when both are finished.
161  DateTime start = DateTime.UtcNow;
162 
163  while (_daeUpdating)
164  {
165  System.Threading.Thread.Sleep(100);
166 
167  if (DateTime.UtcNow - start >= new TimeSpan(0, 0, 1))
168  {
169  //Timeout: Dae is taking too long so carry on
170  _timedOut = true;
171  break;
172  }
173  _timedOut = false;
174  }
175  }
176  catch (Exception err)
177  {
178  Helpers.ErrorLogger.LabViewError("blockUpdater_DoWork", err.Message);
179  }
180  }
181 
188  private void blockUpdater_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
189  {
190  //Raise an event so the GUI can tell when the values have been updated
191  if (OnValuesUpdated != null)
192  {
193  OnValuesUpdated(_timedOut);
194  }
195  }
196 
202  private void _daeUpdater_DoWork(object sender, DoWorkEventArgs e)
203  {
204  _daeUpdating = true;
205  try
206  {
207  //Update the DAE values
208  Standard.Dae.updateRunInformation();
209  _daeUpdating = false;
210  }
211  catch (Exception err)
212  {
213  Helpers.ErrorLogger.SeciError("daeUpdater_DoWork", err);
214  }
215 
216  logStatusBlock(false);
217  }
218 
219  private static void logStatusBlock(Boolean force)
220  {
221  try
222  {
223  //Log the dae status
224  if (_statusBlock != null)
225  {
226  String tempStatus = Seci.Standard.Dae.RunStatus;
227 
228  if (String.IsNullOrEmpty(tempStatus))
229  {
230  tempStatus = "Unknown";
231  }
232 
233  if (force || tempStatus.ToLower() != _statusBlock.CurrentValue.ToLower())
234  {
235  _statusBlock.CurrentValue = tempStatus;
236 
237  _statusBlock.ForceLogForChangesOnly(DateTime.Now, Status.ShortInstrument, Standard.Dae.RunNumber);
238  }
239  }
240  }
241  catch (Exception err)
242  {
243  Helpers.ErrorLogger.LabViewError("daeUpdater_DoWork problem writing status file", err.Message);
244  }
245  }
246 
253  private void _daeUpdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
254  {
255  _daeUpdating = false;
256  }
257 
264  public static void LogValuesAtStartOrEndRun(DateTime time, string runNumber)
265  {
266  //Force log of status block
267  logStatusBlock(true);
268 
269  for (int i = 0; i < Managers.BlockMgr.Blocks.Count; ++i)
270  {
271  Managers.BlockMgr.Blocks[i].ForceLogForChangesOnly(time, Status.ShortInstrument, runNumber);
272  }
273  }
274 
275 
276  #region IDisposable Members
277 
281  public void Dispose()
282  {
283  try
284  {
285  Dispose(true);
286  }
287  finally
288  {
289  GC.SuppressFinalize(this);
290  }
291  }
292 
300  protected virtual void Dispose(bool disposing)
301  {
302  if (disposing)
303  {
304  // Dispose managed resources.
305  if (_blockUpdater != null) _blockUpdater.Dispose();
306  if (_updateTimer != null) _updateTimer.Dispose();
307  }
308 
309  _blockUpdater = null;
310  _updateTimer = null;
311  }
312 
313  #endregion
314  }
315 }
void Dispose()
The dispose method to be called from code.
void PauseUpdater()
Method for pausing the timer. This is done by setting the delay and period to infinity.
This class is used for updating the values SECI has stored for the blocks and from the DAE...
static void logStatusBlock(Boolean force)
This class is used for storing the information for any blocks that are created and provides methods f...
Definition: BlockInfo.cs:18
void ResumeUpdater()
Method for resuming the timer. This is done by setting the delay and period to their normal values...
ValuesUpdatedEventHandler OnValuesUpdated
static void LogValuesAtStartOrEndRun(DateTime time, string runNumber)
Calls the method to log the blocks at the start or end of a run. If logging the end of a run...
Class for holding the standard run-time settings for SECI. A large percentage of the information held...
Definition: Status.cs:12
ValuesUpdater(int updateRate)
Constructor. Initialises the BackgroundWorker object.
void _daeUpdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
The method that is run once the BackgroundWorker thread is finished. This raises an OnValuesUpdated e...
void updateTimer_Tick(object obj)
The method that is called when the timer ticks. If the BackgroundWorker thread is not busy it will re...
virtual void Dispose(bool disposing)
If the value passed it true then the method has been called directly or indirectly from SECI code...
TimerCallback _updateCallBack
static BlockInfo _statusBlock
void _daeUpdater_DoWork(object sender, DoWorkEventArgs e)
The method that is run on the BackgroundWorker thread.
BackgroundWorker _blockUpdater
void blockUpdater_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
The method that is run on the BackgroundWorker thread.
BackgroundWorker _daeUpdater
void StartUpdater()
Method for starting the timer, which fires off the update thread.
void blockUpdater_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
The method that is run once the BackgroundWorker thread is finished. This raises an OnValuesUpdated e...
static Boolean UseDaeMonitorVI
Definition: Status.cs:99