-1

私はいくつかの Windows サービスを c# で作成していますが、ファイルやパスなどの構成ファイルからいくつかの変数を読み取る必要があります。また、タイマー間隔を制御する変数もあります (制御する必要があります)。問題は、構成ファイルのデータが変更されるたびに、そのデータが取得されないことです。たとえば、ファイル名を変更すると、ファイルが存在しないというエラーが表示されます (名前とパスを確認しました)または時間間隔を変更しても何も起こりません。誰か助けてくれませんか?

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;
        }
    }
4

1 に答える 1

0

app.config が格納されているディレクトリを監視する を実装できますFileSystemWatcher。更新されると、何かが変更されたことを通知するイベントがアプリケーションで取得されます。その場合、app.config をリロードして値を更新できます。

リンク: FileSystemWatcher クラス

于 2012-10-17T14:06:55.170 に答える