C#Windows服务计时器应用程序配置在运行时更改

时间:2012-10-17 14:04:34

标签: c# timer windows-services app-config

我在c#中制作了一些Windows服务,我需要从配置文件中读取一些变量,比如文件和路径。我还有一个控制(它应该)定时器间隔的变量。问题是,每次在配置文件中更改数据时,都不会获取该数据,例如,如果我更改文件名,则会收到错误消息,指出该文件不存在(我检查了名称和路径)或如果我改变时间间隔没有任何反应。有人能帮帮我吗?

System.Timers.Timer timer;
    CallWebServices call;
    int time;

    public Planview_SEB_Pervasive()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        call = new CallWebServices();
        startPervasive();
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
        call.InsertLog("PV/S&B Win Service", "Serviço parado", "");
    }

    private void startPervasive()
    {
        try
        {
            try
            {
                //Vai buscar o tempo ao app.config
                time = Convert.ToInt32(ConfigurationManager.AppSettings.Get("TimeElapsedInMinutes"));
            }
            catch (Exception ex)
            {
                //Em caso de falha ficam 5 minutos
                call.InsertLog("PV/S&B Win Service StartPervasive (time)", ex.Message, "");
                time = 5;
            }
            this.timer = new System.Timers.Timer();
            timer.Enabled = true;
            timer.AutoReset = true; //Necesário para que o srviço se repita
            timer.Interval = 1000 * 60 * time; //Cálculo para minutos
            timer.Elapsed += new ElapsedEventHandler(Elapsed);
        }
        catch (Exception ex)
        {
            call.InsertLog("PV/S&B Win Service StartPervasive", ex.Message, "");
            OnStop();
        }
    }

    protected void Elapsed(Object sender, ElapsedEventArgs e)
    {
        try
        {
            timer.Interval = Convert.ToInt32(ConfigurationManager.AppSettings.Get("TimeElapsedInMinutes"));
            StartProcess();
        }
        catch (Exception ex)
        {
            call.InsertLog("PV/S&B Win Service Elapsed", ex.Message, "");
        }
    }

    private static void StartProcess()
    {
        try
        {
            string directory = ConfigurationManager.AppSettings.Get("WorkingDirectory");
            string file = ConfigurationManager.AppSettings.Get("FileToRun");


            //Execução dos processo (ficheiro)
            Process process = new Process();

            process.StartInfo.WorkingDirectory = directory;
            process.StartInfo.FileName = directory + @"\" + file;
            process.StartInfo.Arguments = "";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

            process.StartInfo.CreateNoWindow = false;

            process.StartInfo.RedirectStandardOutput = false;

            process.Start();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以实现一个FileSystemWatcher来监视存储app.config的目录。更新后,您将在应用程序中收到一个事件,通知您某些内容已发生变化。发生这种情况时,您可以重新加载app.config并刷新您的值。

链接:FileSystemWatcher Class