DispatchTimer无法在服务中触发

时间:2018-07-05 15:45:53

标签: c# service

我有一个用C#实现的Windows服务。该服务监视许多目录以保存要保存的文件。但是,为了使其更强大,我尝试让它在一段时间后检查文件夹。

称为Watcher的内部类会初始化fileSystemWatcher和DispatchTimer。

public class Watcher
{
    FileSystemWatcher fileSystemWatcher = null;

    public delegate void TimerInvokedHandler(object sender);
    public event TimerInvokedHandler TimerInvoked;

    public DispatcherTimer Timer { get; set; }

    public int TimerMinutes { get; set; }

    public Watcher()
    {

    }

    public Watcher(String directory, String filter, String dap)
    {
        this.DAP            = dap;
        this.Directory      = directory;
        this.Filter         = filter;
    }

    public Boolean EnableRaisingTimerEvents
    {
        get { return this.Timer.IsEnabled;  }
        set
        {
            this.Timer.IsEnabled = value;
            if (value)
            {
                this.Timer.Start();
                Log("Timer Started");
            }
            else
            {
                this.Timer.Stop();
                Log("Timer Stopped");
            }

        }
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        StopWatch();
        TimerInvoked?.Invoke(sender);
    }

    public void StartWatch()
    {
        if (fileSystemWatcher == null)
        {
            fileSystemWatcher = new FileSystemWatcher();
            fileSystemWatcher.Filter = Filter;
            fileSystemWatcher.Path = Directory;

            fileSystemWatcher.Created += FileSystemWatcher_Created;
            fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;

            fileSystemWatcher.Error += FileSystemWatcher_Error;
        } 

        if (this.Timer == null)
        {
            Log("Initialising Timer");

            this.Timer = new DispatcherTimer();
            this.Timer.Interval = new TimeSpan(0, this.TimerMinutes, 0);
            this.Timer.Tick += Timer_Tick;
        }

        this.EnableRaisingFileSystemsEvents = true;
        this.EnableRaisingTimerEvents = true;

        Log(String.Format("Watching Directory {0}", Directory));
    }

    // stops timer and file system events
    public void StopWatch()
    {
        if (fileSystemWatcher != null) 
            EnableRaisingFileSystemsEvents = false;

        if (Timer != null)
        {
            EnableRaisingTimerEvents = false;
        }

        Log(String.Format("Watching {0} Switched Off", this.Directory));
    }


    private void Log ( String Message )
    {
        LogEvent?.Invoke(this, Message);
    }

}

外部类基于ServiceBase创建这些观察者的列表,因为它是服务,因此仅当它在Windows Service Manager中停止时才结束

                foreach (nhs_acquisition_profile pfl in p)
                {
                    Watcher w = null;
                    String filePattern = String.Empty;

                    try
                    {

                        profileWatcherLog.WriteEntry(String.Format("Attempting to set-up watcher on {0} for DAP {1}",pfl.dap_file_location,pfl.dap_name));

                        if (pfl.dap_acquisition_method_loca.ToLower() == "xml") w = new Watcher(pfl.dap_file_location, "*.xml", pfl.dap_name);
                        else w = new Watcher(pfl.dap_file_location, "*.*", pfl.dap_name);

                        profileWatcherLog.WriteEntry("Initialising Event Handlers");
                        // initialise event handlers
                        w.FileCreated   += W_FileCreated;
                        w.FileRenamed   += W_FileRenamed;
                        w.TimerInvoked  += W_TimerInvoked;
                        w.LogEvent      += W_LogEvent;
                        profileWatcherLog.WriteEntry("Event Handlers initialised");

                        // dispatch timer

                        w.TimerMinutes = Convert.ToInt32(Properties.Settings.Default.TimerDelay);

                        w.StartWatch();

                        profileWatcherLog.WriteEntry("Watch started....Adding to Watcher List");

                        // add the watcher to the list of watchers
                        FileWatcherList.Add(w);

                        profileWatcherLog.WriteEntry("Added to list of file watchers");

                        profileWatcherLog.WriteEntry(String.Format("Watching {0} for files matching *.* for DAP {1}",pfl.dap_file_location,pfl.dap_name));
                    }
                    catch
                    {
                        throw;
                    }
                }

变量FileWatcherList是ProfileWatcher类的一个字段,它构成整个服务。

我发现的是DispatchTimer Tick事件从未发生。我可以肯定地说,这不是DispatchTimer在Tick之前被丢弃的一个实例,但是看不到为什么它不触发。

1 个答案:

答案 0 :(得分:0)

Dmitri是正确的,我应该一直使用Timer而不是DispatchTimer。一旦我移到那,它就可以正常工作。