I have a windows service that prints reports on start.I have two scenarios-
1.I want to start this service when a new folder has been created in my E:\here\ folder.How to accomplish this?
2 Also is it possible to trigger a service when a new row inserted into database table?how?
Please provide links for above queries.
You can use a FileSystemWatcher to check if the specific folder is created in a specific location. When its created you can start the Windows Service.
Creating FileSystemWatcher
//Define this in the clas level
FileSystemWatcher watcher = new FileSystemWatcher();
Initializing
private void InitializeWatcher()
{
watcher.Created += new FileSystemEventHandler(NotifyChange);
watcher.Path = @"E:\here\folder";
watcher.EnableRaisingEvents = true;
}
Starting Windows Service when folder is created
private void NotifyChange(object sender, FileSystemEventArgs e)
{
if (e.Name.Equals("FolerName",StringComparison.OrdinalIgnoreCase))
{
new ServiceController("WindowsServiceName").Start();
}
}
You can create a new service which polls for new records and/or new folders, and then starts the existing service accordingly.
For the database, most likely you'd simply verify the row count or the latest ID in a given table periodically, for instance every 5 seconds or whatever your acceptable window is. If your database is a SQL Server database, you can also use the SqlDependency class. There is more about that on MSDN (http://msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx) and the Code Project (http://www.codeproject.com/Articles/12335/Using-SqlDependency-for-data-change-events).
For the folder, you can use the FileSystemWatcher class to notify you of any changes (here is an example of how to do that: http://snipplr.com/view/54606).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With