If you need an application-wide update, and it's not going to cause too much delay to do the update, the best option is to start off a background Thread
that can run and post events to subscribers that the data has been refreshed (possibly first check to see if it has been altered using some specific logic too by adding a custom EventArgs
to the event).
For example, create an instance of this class somewhere top-level (perhaps your main form code?):
using System;
using System.Threading;
using System.Windows.Forms;
public class PollerThread
{
private Thread _thread;
private SynchronizationContext _syncContext;
public event EventHandler UpdateFinished;
public PollerThread()
{
_syncContext = WindowsFormsSynchronizationContext.Current;
ThreadStart threadStart = new ThreadStart(ThreadStartMethod);
_thread = new Thread(threadStart);
_thread.Name = "UpdateThread";
_thread.IsBackground = true;
_thread.Priority = System.Threading.ThreadPriority.Normal;
}
public void Start()
{
if ((_thread.ThreadState & ThreadState.Unstarted) == ThreadState.Unstarted)
_thread.Start();
else
throw new Exception("Thread has already been started and may have completed already.");
}
public void ThreadStartMethod()
{
try
{
while (true)
{
// Go get the new data from the SQL server
OnUpdateFinished(); // Notify all subscribers (on their own threads)
Thread.Sleep(10000); // 10 sec wait before the next update
}
}
catch (ThreadAbortException)
{
// The thread was aborted... ignore this exception if it's safe to do so
}
}
protected virtual void OnUpdateFinished()
{
if (UpdateFinished != null)
{
SendOrPostCallback method = new SendOrPostCallback(
delegate(object state)
{
UpdateFinished(this, EventArgs.Empty);
});
_syncContext.Send(method, null);
}
}
}
Subscribe each of the areas that need to respond to new updates to the UpdateFinished
event. This will execute on the thread used to construct the PollerThread
class.
This answer may sound a little loose, and applies more specifically to windows forms projects, but the usefulness really depends on your current implementation. At least you can build on this anyway. Hope it helps :)