SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
LVMQUpdater.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 
7 namespace Seci.LabView
8 {
17  class LvmqUpdater : IDisposable
18  {
19  private BackgroundWorker _backgroundUpdater;
20  private Timer _updateTimer;
21  private TimerCallback _updateCallBack;
22  private int _updateRate = 3000;
23 
24  private Boolean _messagesChanged;
25 
29  public event EventHandler OnChangeToLVMessages;
30  public event EventHandler OnLabviewNotThere;
31 
36  public LvmqUpdater(int updateRate)
37  {
38  if (updateRate <= 0)
39  {
40  _updateRate = 3000;
41  }
42 
43  if (_backgroundUpdater == null)
44  {
45  //Initialise the background worker
46  _backgroundUpdater = new BackgroundWorker();
47  _backgroundUpdater.DoWork += new DoWorkEventHandler(backgroundUpdater_DoWork);
48  _backgroundUpdater.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundUpdater_RunWorkerCompleted);
49  }
50  }
51 
55  public void StartUpdater()
56  {
57  if (_updateCallBack == null && _updateTimer == null)
58  {
59  _updateCallBack = new TimerCallback(updateTimer_Tick);
60  _updateTimer = new Timer(_updateCallBack, null, 0, _updateRate);
61  }
62  }
63 
67  public void PauseUpdater()
68  {
69  if (_updateTimer != null)
70  {
71  _updateTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
72  }
73  }
74 
78  public void ResumeUpdater()
79  {
80  if (_updateTimer != null)
81  {
82  _updateTimer.Change(0, _updateRate);
83  }
84  }
85 
91  private void updateTimer_Tick(object obj)
92  {
93  //Only update if not already running background thread
94  try
95  {
96  if (!_backgroundUpdater.IsBusy)
97  {
98  _backgroundUpdater.RunWorkerAsync();
99  }
100  }
101  catch (Exception err)
102  {
103  Seci.Helpers.ErrorLogger.SeciError("LvmqUpdater:updateTimer_Tick", err);
104  }
105  }
106 
112  private void backgroundUpdater_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
113  {
114  try
115  {
116  //Check that LabVIEW exists
117  if (!LabViewApp.AreYouThere())
118  {
119  //Raise an event to notify the user
120  if (OnLabviewNotThere != null)
121  {
122  OnLabviewNotThere(sender, e);
123  }
124  }
125  else
126  {
127  _messagesChanged = Standard.MessageQueue.UpdateMessages();
128  }
129 
130  }
131  catch(Exception err)
132  {
133  Seci.Helpers.ErrorLogger.SeciError("LvmqUpdater:backgroundUpdater_DoWork", err);
134  }
135  }
136 
143  private void backgroundUpdater_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
144  {
145  //Raise an event so the GUI can tell when new messages are received.
146  if (_messagesChanged && OnChangeToLVMessages != null)
147  {
148  OnChangeToLVMessages(sender, e);
149  }
150  }
151 
152 
153  #region IDisposable Members
154 
158  public void Dispose()
159  {
160  try
161  {
162  Dispose(true);
163  }
164  finally
165  {
166  GC.SuppressFinalize(this);
167  }
168  }
169 
177  protected virtual void Dispose(bool disposing)
178  {
179  if (disposing)
180  {
181  // Dispose managed resources.
182  if (_backgroundUpdater != null) _backgroundUpdater.Dispose();
183  if (_updateTimer != null) _updateTimer.Dispose();
184  }
185 
186  _backgroundUpdater = null;
187  _updateTimer = null;
188  }
189 
190  #endregion
191  }
192 }
void updateTimer_Tick(object obj)
The method that is called when the timer ticks. If the BackgroundWorker thread is not busy it will re...
Definition: LVMQUpdater.cs:91
EventHandler OnChangeToLVMessages
Event for indicating to the GUI level that the values were updated.
Definition: LVMQUpdater.cs:29
This class contains the DCOM connection to the LabVIEW application and stores a reference to all the ...
Definition: LabViewApp.cs:20
This class is used for checking to see if the LabVIEW messages received from the message panel VI hav...
Definition: LVMQUpdater.cs:17
LvmqUpdater(int updateRate)
Constructor. Initialises the BackgroundWorker object.
Definition: LVMQUpdater.cs:36
BackgroundWorker _backgroundUpdater
Definition: LVMQUpdater.cs:19
TimerCallback _updateCallBack
Definition: LVMQUpdater.cs:21
static Boolean AreYouThere()
Definition: LabViewApp.cs:203
void backgroundUpdater_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
The method that is run on the BackgroundWorker thread.
Definition: LVMQUpdater.cs:112
void PauseUpdater()
Method for pausing the timer. This is done by setting the delay and period to infinity.
Definition: LVMQUpdater.cs:67
EventHandler OnLabviewNotThere
Definition: LVMQUpdater.cs:30
void StartUpdater()
Method for starting the timer that fires off the update thread.
Definition: LVMQUpdater.cs:55
void ResumeUpdater()
Method for resuming the timer. This is done by setting the delay and period to their normal values...
Definition: LVMQUpdater.cs:78
void backgroundUpdater_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
The method that is run once the BackgroundWorker thread is finished. This raises an OnChangeToLVMessa...
Definition: LVMQUpdater.cs:143
void Dispose()
The dispose method to be called from code.
Definition: LVMQUpdater.cs:158
virtual void Dispose(bool disposing)
If the value passed it true then the method has been called directly or indirectly from SECI code...
Definition: LVMQUpdater.cs:177