patterncsharpMinor
File/folder watcher Windows service
Viewed 0 times
filewatcherservicefolderwindows
Problem
I am developing a C# Windows service that will always watch different folders/files and DB query results on different time intervals. There can be dozens of watchers each watching a specific file or folder or DB query results and sends emails at specific email address if some predefined threshold is met, and begins watching again.
The requirements that I'm trying to address are:
The strategy I am using:
-
I create a List of
-
I call a method
-
I surrounded thread method's body with try/catch to catch any exception.
-
If an exception occurs, I create a
-
Upon occurring of the 'Elapsed' event of this timer, I recreate another task and add it to tasks' dictionary.
Dictionary object for holding tasks against a watcher's ID:
Here
```
class TaskDetails
{
//The Task object
public Task WatcherTask { get; set; }
//CancellationTokenSource reference for each task ..
//if we need to cancel tasks individually
public CancellationTokenSource WatcherCancellationToken { get; set; }
//It a Timer that will enable this specific t
The requirements that I'm trying to address are:
- Each Watcher must do its task in a separate Thread.
- If a watcher/tasks throws an exception, it must automatically be restarted after, say, 3 hours.
The strategy I am using:
-
I create a List of
IWatcher objects which holds many instances of DB, File and Folder watchersIWatcher is just an interface with some common properties/methods across all DB/File/Folder watchers-
I call a method
BeginWatch which creates a separate task for each watcher in watcher's list and stored that task in a Dictionary. The key is set to the ID of watcher.-
I surrounded thread method's body with try/catch to catch any exception.
-
If an exception occurs, I create a
Timer object in the catch() body, save the watcher's ID (whose work is stopped) in this timer and schedule it with its interval set to a few hours. I remove the task object from tasks' dictionary.-
Upon occurring of the 'Elapsed' event of this timer, I recreate another task and add it to tasks' dictionary.
Dictionary object for holding tasks against a watcher's ID:
private Dictionary _watcherThreadsHere
string: DB/File or Folder watcher's unique ID
TaskDetails: A class that holds aTaskand other information for that task that I need during program execution.
TaskDetails:```
class TaskDetails
{
//The Task object
public Task WatcherTask { get; set; }
//CancellationTokenSource reference for each task ..
//if we need to cancel tasks individually
public CancellationTokenSource WatcherCancellationToken { get; set; }
//It a Timer that will enable this specific t
Solution
Took a quick glance, seems relatively logical. Can't think of anything I would do wildly differently.
I do have one suggestion, though:
Subscribe to the
I do have one suggestion, though:
Subscribe to the
System.Threading.Tasks.TaskScheduler.UnobservedTaskException event handler. In case you do a mistake or something strange happens with a Task, then you can catch that error, log it (stack trace), and keep your service running or restart it or whatever is necessary. If you don't subscribe to that event, your service will simply be closed and you have nothing to help you debug, plus you don't get any notifications.Context
StackExchange Code Review Q#25972, answer score: 2
Revisions (0)
No revisions yet.