我创建了一个在后台特定时间执行exe的服务。我想设置一个计时器,使服务每天同时执行exe ..如何做到这一点.. ???这是我在服务类中编写的代码:
public partial class Service1 : ServiceBase
{
string time = DateTime.Now.ToShortTimeString();
Process p = new Process();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(DoWork);
bw.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
p.StartInfo = new ProcessStartInfo(@"c:\windows\ccm\ccmeval.exe");
if (time == "6:55 PM")
{
p.Start();
p.WaitForExit();
base.Stop();
}
}
protected override void OnStop() { }
答案 0 :(得分:0)
假设您希望每天早上6:30运行每项任务
var dtTimeToStartAt = DateTime.Today.AddHours(6).AddMinutes(30);
dtTimeToStartAt = DateTime.Now > dtTimeToStartAt ? dtTimeToStartAt.AddDays(1) : dtTimeToStartAt;
var timer = new System.Threading.Timer(x => YourCallbackMethod(), new object() /*not much use*/,
dtTimeToStartAt.Subtract(DateTime.Now), new TimeSpan(24, 0, 0));