문제

I created this windows-service. My boss wants to be able to pause it by using an appsettings in the config-file. It should take immediate effect without having to restart the service.

In my config-file I have this :

<appSettings>
    <!-- If you want the DIS to pause for a while, give a valid number here.
    The value should be provided in minutes.-->     
    <add key="PauseDis" value="5"/>
</appSettings>

In my code I'm doing the following:

protected override void OnStart(string[] args)
    {
        thread = new Thread(WorkerThreadFunc);
        thread.Name = "Indigo.DataIntakeService Thread";
        thread.IsBackground = true;
        thread.Start();
    }

private void WorkerThreadFunc()
    {
        while (!shutdownEvent.WaitOne(0))
        {
            CheckFolders(toCheckFolders);
        }
    }private void CheckFolders(FoldersConfigSection folder)
    {
        using (FolderActions folderActions = new FolderActions())
        {
            PauseWorking();

            folderActions.DestinationFolders = (FoldersConfigSection)ConfigurationManager.GetSection("DestinationFolders");
            folderActions.BackUpFolders = (FoldersConfigSection)ConfigurationManager.GetSection("BackUpFolders");
            folderActions.TriggerName = ConfigurationManager.AppSettings["Trigger"];

            foreach (FolderElement folderElement in folder.FolderItems)
            {
                folderActions.SearchDirectoryAndCopyFiles(folderElement.Path, ConfigurationManager.AppSettings["Environment"]);
            }
        }
    }

    private void PauseWorking()
    {
        this.pauseTime = Convert.ToInt16(ConfigurationManager.AppSettings["PauseDis"]);
        LogManager.LogWarning(String.Format("PauseTime => {0}", this.pauseTime));

        if (this.pauseTime != 0)
        {
            // A pause was provided in the config-file, so we pause the thread.
            // The pause-time is provided in minutes. So we convert it to mil!iseconds.
            // 1 minute = 60 000 milliseconds
            LogManager.LogWarning(String.Format(Resources.WARN_ThreadSleeping, this.pauseTime));
            Thread.Sleep(this.pauseTime * 60000);
        }
    }

But I must be doing something wrong, because it doesn't read the setting again. It just takes what is in memory.

도움이 되었습니까?

해결책

There is a method on the ConfigurationManager class called RefreshSection.

Refreshes the named section so the next time that it is retrieved it will be re-read from disk.

 ConfigurationManager.RefreshSection("AppSettings");

The problem is, if I understand it correctly, that you set this new value outside the service that read it. So you will be forced to call this RefreshSection just before reading the value and this can be a problem for the performance of your app.

다른 팁

And you will have to set config value to zero or it will pause again next loop.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top