【问题标题】:C# Service Run Process on conditionC# 服务在条件下运行进程
【发布时间】:2013-08-02 10:50:56
【问题描述】:

原始问题: 到目前为止,我有以下代码可用于测试目的。 我需要能够在启动后运行我的代码中的进程,并且只有在我的 DHCP 租约被更新/释放或(我想检查 IP 地址的变化时)。 话虽如此,我需要帮助的事情是:

  • ONE:如何让线程任务按时间间隔执行,(**下面的评论提供了一些帮助**)

    • 二: 确定自服务启动以来我的 DHCP 租约是否已更改/续订/释放。 在感谢您之前,如有任何混淆,我深表歉意。*

编辑更新:我已经更新了我的代码,老实说,我已经弄清楚了计时器,我只需要帮助找出最好的方法来了解 dhcp 租约何时或是否已续订/发布和续订/IP 比以前发生了变化,因此我可以查看是否在检查定时间隔时它是否已更改为运行 exe。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.ServiceProcess;
using System.IO;
using System.Timers;

namespace MyWindowsService
{
  class Program : ServiceBase
  {
    private static Process p = new Process();
    private static System.Timers.Timer aTimer;
    //private static Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
    static void Main(string[] args)
    {
      //Set the location of the DHCP_Opion text creater.
      p.StartInfo = new ProcessStartInfo(@"C:\NetLog\DHCPSolution-Option120.exe");
      ServiceBase.Run(new Program());
    }

    public Program()
    {
      this.ServiceName = "New_Service_Test";
      p.Start();
      // Create a timer with a ten second interval.
      aTimer = new System.Timers.Timer(30000);

      // Hook up the Elapsed event for the timer.
      aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

      // Set the Interval to 2 seconds (2000 milliseconds).
      aTimer.Interval = 30000;
      aTimer.Enabled = true;
      //Garbarge collection.
      GC.KeepAlive(aTimer);
    }

    protected override void OnStart(string[] args)
    {
      //TODO: place your start code here
      base.OnStart(args);
    }

    protected override void OnStop()
    {
      //TODO: clean up any variables and stop any threads
      base.OnStop();
    }

    // Specify what you want to happen when the Elapsed event is  
    // raised. 
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
         p.Start();
         p.WaitForExit();
    }
  }
}

已编辑:澄清。

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 这个问题应该稍微改一下..你能用3句话说明你想要达到的目标吗?
  • 如果我正确理解您想要什么,请查看 System.Timers.Timer。当您的流程开始时,启动一个计时器,每隔 X 秒或每分钟检查一次您想要的内容,并在满足您的条件后启动一个流程。 msdn.microsoft.com/en-us/library/system.timers.timer.aspx
  • 具体说明你击中的地方和你需要什么,说清楚!

标签: c# .net service


【解决方案1】:

我不清楚您是想在租约到期并续订时运行程序,还是仅在 IP 地址更改时运行程序。

如果你想知道IP地址是什么时候改变的,你在程序启动时获取IP地址,然后每隔一个定时器间隔检查一次。所以,你会写:

private static string CurrentIPAddress;

public Program()
{
    this.ServiceName = "New_Service_Test";
    CurrentIpAddress = LocalIPAddress();
    p.Start();

    // initialize timer, etc.
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    string myIp = LocalIPAddress();
    if (myIp == CurrentIPAddress)
    {
        // hasn't changed.
        break;
    }
    CurrentIpAddress = myIp;
    p.Start();
    p.WaitForExit();
}

LocalIpAddress方法来自https://stackoverflow.com/a/6803109/56778

您可以通过使用NetworkChange 类注册事件处理程序来消除所有轮询。每当 IP 地址更改时,NetworkAddressChanged 事件就会触发。 MSDN 主题有一个很好的例子。

如果你想确定 DHCP 租约何时更新,即使 IP 地址没有改变,你可能不得不深入研究 WMI 接口,我对此知之甚少。

【讨论】:

  • 网络地址更改最终成为一个可行的解决方案。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多