Say I have a windows service that runs a method to generate reports.
For example say I have a Reports table that has the report path, status, name of report, parameters etc.
When a user clicks the generate report button a new entry will be added to the table with a status of queued. The service will take queued reports, generate the report, update the status and set the path of the completed report.
The 2 ways I can think of doing this is to either poll the table for queued reports (something like):
TimerCallback callback = new TimerCallback(RunQuery);
this.QueryTimer = new Timer(callback, null, 1000, 10000);
public void RunQuery(object obj)
{
//find reports with status of queued,
//loop through them and generate reports
}
Or create a file with the ReportId and use a FileSystemWatcher to determine which reports to run (something like):
private void FileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
ReportStack.Push(e.FullPath);
Thread TR = new Thread(RunQuery);
TR.Start();
}
public void RunQuery()
{
string filePath = Convert.ToString(ReportStack.Pop());
GenerateReport(filePath);
...
}
A downside to the 1st method is that I need to specify a poll time and reports don’t generate instantly as they are queued. A downside to the second method is the small hassle of creating and deleting files, setting up permission for the app to write to a shared folder etc.
Is there a way to get the service to automatically kick off reports as they are placed into the report table (something like a DatabaseTableWatcher!) or another better way to do this?
SQL triggers or Message Queuing
http://msdn.microsoft.com/en-us/library/ms189799.aspx
So when a record is inserted into the table you can fire off an action such as calling a sproc.
Personally I like your first polling approach.