我创建了这个windows-service。我的老板希望能够通过使用配置文件中的应用程序设置来暂停它。它应该立即生效,而不必重新启动服务。
在我的配置文件中,我有以下内容:
<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>在我的代码中,我做了以下事情:
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);
}
}但我一定是做错了什么,因为它不会再次读取设置。它只需要内存中的内容。
发布于 2013-06-24 17:47:53
在ConfigurationManager类上有一个名为RefreshSection的方法。
刷新已命名的部分,以便下次检索时将从磁盘重新读取它。
ConfigurationManager.RefreshSection("AppSettings");问题是,如果我理解正确的话,您在读取它的服务之外设置了这个新值。因此,您将被迫在读取该值之前调用此RefreshSection,这可能会对您的应用程序的性能造成问题。
发布于 2013-06-24 18:33:42
您必须将配置值设置为零,否则它将在下一次循环中再次暂停。
https://stackoverflow.com/questions/17272425
复制相似问题