【发布时间】: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
-
具体说明你击中的地方和你需要什么,说清楚!