DispatchTimer не работает в сервисе - PullRequest
0 голосов
/ 05 июля 2018

У меня есть служба Windows, реализованная в C #. Служба просматривает несколько каталогов для файла, который будет сохранен. Однако, чтобы сделать его более надежным, я пытаюсь проверить его после определенного периода времени.

Внутренний класс, называемый 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, который формирует службу в целом.

Я обнаружил, что событие Tick DispatchTimer никогда не происходит. Я вполне уверен, что это не тот случай, когда DispatchTimer был утилизирован до тика, но я не могу понять, почему он не запускается.

1 Ответ

0 голосов
/ 20 июля 2018

Дмитрий прав, я должен был использовать Таймер, а не Диспетчер Таймер. Как только я перешел к этому, он работал нормально.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...