Expert Advice Needed to Develop a Windows C # Service That Will Use / Create Many Threads

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 { //This will keep list of all active threads ... as I will be communicating with them later private List<Thread> _watcherThreads; public WatcherThreadsManager() { this._watcherThreads = new List<Thread>(); } //I will call this method and pass in any watcher which i want to run in a new thread public void CreateWatcherThread(IWatcher watcher) { Thread _watcher = new Thread(_createWatcherThread); //the Copy() will produce its deeply copied copy ... i wanted to make sure thread works on a free object .. not sure _watcher.Start(watcher.Copy()); //Add Thread to threads' list this._watcherThreads.Add(_watcher); } private void _createWatcherThread(object wat) { IWatcher watcher = wat as IWatcher; try { //the following function will begin the watch. //I dont want any loop either .. as i want to call the BeginWatch() method only once. watcher.BeginWatch(); } catch (Exception ex) { // i should be able to inform the parent thread about the exception so this thread is set to sleep. as we can reactivate it later when the issue fixes. // how to do that? } } } 

What is the best way to achieve what I want?

+4
source share
2 answers

You can fulfill all your requirements using Tasks, here are some links for further reading:

Good luck

+4
source

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 delete, only this thread can be stopped / killed

It sounds like you don't need threads, you need to catch exceptions. Why don't you do it?

+1
source

Source: https://habr.com/ru/post/1478999/


All Articles