开发一个使用/创建许多线程的c# Windows Service需要专家意见

本文关键字:Service Windows 专家 多线程 一个 创建 开发 | 更新日期: 2023-09-27 18:12:04

我正在开发c# windows服务,它将监视几个文件,文件夹和DB表的任何更改。有三种类型的观察者(我称之为观察者)。

  1. FileWatcher:使用。net FileSystemWatcher持续监视文件,并在发送警报时引发事件。警报发送后,手表功能恢复。

  2. FolderWatcher:使用。net Timer不断监视文件夹,并引发一个事件,在某些条件下发送警报。警报发送后,手表功能恢复。

  3. DBWatcher:每分钟(Timer)执行一次SQL查询,如果结果错误则发送一个警报。

你可以猜到,当windows服务运行时,这些所有的观察者都会一直运行。

它们都实现了一个接口IWatcher,并提供了一个方法BeginWatch,该方法执行每个监视程序所需的操作,例如每分钟使用计时器查询DB(如果监视程序是DBWatcher)。创建这些监视器的输入是一个XML文件。它包含如下内容:

<?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>

这个XML告诉我们要创建多少个观察者。可以创建几十个观察者。

由于我们面临的一些问题,我们决定每个监视器将在不同的线程上运行。因此,在未处理异常的情况下,只有该线程可以被停止/杀死,我们通过电子邮件通知IT部门有关情况的警报。我们以后一定能恢复。

现在我很困惑。因为有线程,异步任务,池线程,后台线程等,我应该使用什么??/I ' m new to this threading.

让我告诉你我的要求,然后你可以指导我一些适当的解决方案:

  1. 我希望每个观察者在一个单独的线程中运行。
  2. 线程必须连续运行,我的监视器必须能够监视到windows服务本身关闭。
  3. 线程必须能够与其父线程(每个线程创建的类)通信以更新其状态。
  4. 线程中任何未处理的异常必须被线程本身捕获,然后传递给父类(使用第3点)。

我已经创建了下面的类,它将负责创建、通信和管理所有线程:

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?
        }
    }
} 

实现我想要的最好的方法是什么?

开发一个使用/创建许多线程的c# Windows Service需要专家意见

你可以通过使用Tasks来实现你所有的需求,这里有一些链接供你进一步阅读:

  1. 创建线程- Task.Factory.StartNew vs new Thread()

  2. 创建长运行任务最佳实践

  3. 任务报告进度

  4. 处理任务异常

祝你好运!

由于我们面临的一些问题,我们决定每个监视器将在不同的线程上运行。因此,在未处理异常的情况下,只有该线程可以被停止/终止

听起来好像你不需要线程,你只需要捕获异常。你为什么不这么做呢?