[英]Timer in C# to start an .exe
I am trying to make a program that will wait for 8 hours and then我正在尝试制作一个等待 8 小时的程序,然后
if it's 8am or later - run an.exe and close itself (done)如果是早上 8 点或更晚 - 运行 an.exe 并自行关闭(完成)
if it's not 8am yet - wait until 8am and then run.exe and close itself如果还不是早上 8 点 - 等到早上 8 点,然后运行.exe 并自行关闭
My code:我的代码:
namespace Timer
{
public partial class Timer : Form
{
public Timer()
{
InitializeComponent();
}
TimeSpan eight = new TimeSpan(8, 0, 0);
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan now = DateTime.Now.TimeOfDay;
if (now >= eight)
{
System.Diagnostics.Process.Start(@"Alarm.exe");
System.Windows.Forms.Application.Exit();
}
else
{
// wait until 8am and then:
System.Diagnostics.Process.Start(@"Alarm.exe");
System.Windows.Forms.Application.Exit();
}
It's probably very easy, but I am a complete beginner and I can't figure this out myself:/这可能很容易,但我是一个完整的初学者,我自己无法弄清楚:/
Thank you very much for your help.非常感谢您的帮助。
very low tech approach非常低技术的方法
var now = DateTime.Now;
var schedule = now.AddHours(8);
if (schedule.TimeOfDay < TimeSpan.FromHours(8))
schedule = schedule.Date + TimeSpan.FromHours(8);
var timer = new Timer(Callback, null, schedule - now, Timeout.InfiniteTimeSpan)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.