I am developing a C # windows service that will monitor multiple DB files, folders and tables for any changes. There are three types of observers (I call them observers).
FileWatcher . Constantly monitors the file using the .Net FileSystemWatcher and generates an event in which a warning will be sent. After sending an alert, the view function resumes.
FolderWatcher : A constant watcher for a folder using the .Net timer and raises an event in which a warning should be sent under certain conditions. After sending an alert, the view function resumes.
DBWatcher : Executes an SQL query after every minute (Timer) and sends a warning if the result is incorrect.
You can guess that all observers will work all the time when the Windows service is running.
All of them implement the IWatcher interface and provide the BeginWatch method, which performs the operations necessary for each observer, for example, it queries the database every minute using a timer (if the observer is DBWatcher). The input to create these observers is an XML file. It contains the following:
<?xml version="1.0" encoding="utf-8"?> <watchlist> <watcher type="FolderWatcher"> <path>D:\Projects\.Net\V1\</path> <PauseInMinBtwAlerts>1</PauseInMinBtwAlerts> <alert> xxx@xxx.com </alert> </watcher> <watcher type="FileWatcher"> <path>D:\Projects\.Net\</path> <fileName>e.txt</fileName> <alert> yyy@yyy.com </alert> </watcher> <watcher type="DBWatcher"> <sqlquery>select blah blah blah ... </sqlquery> <connectionstring>connection string for the DB</connectionstring> <alert> yyy@yyy.com </alert> </watcher> </watchlist>
This XML tells us how many watchers should be created. Dozens of observers are being created.
Due to some problems that we encountered, we decided that each observer would be launched on a different Thread. Thus, in the case of an unhandled release, only this thread can be stopped / killed, and we will inform the IT department via an email alert about the situation. We should be able to resume it later.
Now I'm confused. as there are Threads, Async tasks, merged threads, background threads, etc. that I should use to create this streaming. // m.
Let me tell you my requirement, and then you can lead me to some correct solutions:
- I want each observer to run in a separate thread.
- The topic should run continuously, and my supervisor should be able to watch until the Windows service itself shuts down.
- Thread should be able to communicate with its parent thread (the class where each thread is created) in order to update it about its status.
- Any unhandled exception in the stream must be caught in the stream itself and then passed to the parent class (using the third point).
I created the following class, which will be responsible for creating, communicating and managing all threads:
public class WatcherThreadsManager {
What is the best way to achieve what I want?