C#如何在给定时间运行代码?

C#如何在给定时间运行代码?,c#,C#,简单地说, 我早上开始运行我的C#程序,该程序应该在下午5:45向用户显示一条消息。我怎样才能在C#中做到这一点 编辑:我问这个问题是因为我认为使用计时器不是最好的解决方案(定期比较当前时间和运行任务所需的时间): 你也可以用 还有一个类可以帮助你,你也可以使用它 还有一个类可以帮助您您可以使用计时器检查DateTime.Now==(您想要的具体时间)是否为每分钟 这是一个使用windows窗体的代码示例 public MainWindow() { InitializeC

简单地说,

我早上开始运行我的C#程序,该程序应该在下午5:45向用户显示一条消息。我怎样才能在C#中做到这一点

编辑:我问这个问题是因为我认为使用计时器不是最好的解决方案(定期比较当前时间和运行任务所需的时间):

你也可以用

还有一个类可以帮助你,你也可以使用它


还有一个类可以帮助您

您可以使用计时器检查DateTime.Now==(您想要的具体时间)是否为每分钟

这是一个使用windows窗体的代码示例

public MainWindow()
    {
        InitializeComponent();
        System.Windows.Threading.DispatcherTimer timer_1 = new System.Windows.Threading.DispatcherTimer();
        timer_1.Interval = new TimeSpan(0, 1, 0);
        timer_1.Tick += new EventHandler(timer_1_Tick);
        Form1 alert = new Form1();
    }
    List<Alarm> alarms = new List<Alarm>();

    public struct Alarm
    {
        public DateTime alarm_time;
        public string message;
    }


    public void timer_1_Tick(object sender, EventArgs e)
    {
        foreach (Alarm i in alarms) if (DateTime.Now > i.alarm_time) { Form1.Show(); Form1.label1.Text = i.message; }
    }
public主窗口()
{
初始化组件();
System.Windows.Threading.dispatchermer timer_1=新的System.Windows.Threading.dispatchermer();
timer_1.Interval=新的时间跨度(0,1,0);
timer_1.Tick+=新事件处理程序(timer_1_Tick);
Form1警报=新Form1();
}
列表报警=新列表();
公共结构报警
{
公共日期时间报警时间;
公共字符串消息;
}
公共无效计时器\u 1 \u勾号(对象发送方,事件参数e)
{
foreach(报警中的报警i)if(DateTime.Now>i.Alarm_time){Form1.Show();Form1.label1.Text=i.message;}
}

如果DateTime.Now==(您想要的具体时间),您可以使用计时器检查每分钟

这是一个使用windows窗体的代码示例

public MainWindow()
    {
        InitializeComponent();
        System.Windows.Threading.DispatcherTimer timer_1 = new System.Windows.Threading.DispatcherTimer();
        timer_1.Interval = new TimeSpan(0, 1, 0);
        timer_1.Tick += new EventHandler(timer_1_Tick);
        Form1 alert = new Form1();
    }
    List<Alarm> alarms = new List<Alarm>();

    public struct Alarm
    {
        public DateTime alarm_time;
        public string message;
    }


    public void timer_1_Tick(object sender, EventArgs e)
    {
        foreach (Alarm i in alarms) if (DateTime.Now > i.alarm_time) { Form1.Show(); Form1.label1.Text = i.message; }
    }
public主窗口()
{
初始化组件();
System.Windows.Threading.dispatchermer timer_1=新的System.Windows.Threading.dispatchermer();
timer_1.Interval=新的时间跨度(0,1,0);
timer_1.Tick+=新事件处理程序(timer_1_Tick);
Form1警报=新Form1();
}
列表报警=新列表();
公共结构报警
{
公共日期时间报警时间;
公共字符串消息;
}
公共无效计时器\u 1 \u勾号(对象发送方,事件参数e)
{
foreach(报警中的报警i)if(DateTime.Now>i.Alarm_time){Form1.Show();Form1.label1.Text=i.message;}
}

您可以轻松实现自己的报警类。首先,您可能需要检查MS文章末尾的。

您可以轻松实现自己的报警类。首先,您可能需要查看MS文章末尾的

我问这个问题是因为我认为使用计时器不是最好的解决方案(定期比较当前时间和运行任务所需的时间)

为什么??为什么不是最好的解决方案呢?IMO定时器是最好的解决方案。但不是你实施的方式。尝试以下方法

private System.Threading.Timer timer;
private void SetUpTimer(TimeSpan alertTime)
{
     DateTime current = DateTime.Now;
     TimeSpan timeToGo = alertTime - current.TimeOfDay;
     if (timeToGo < TimeSpan.Zero)
     {
        return;//time already passed
     }
     this.timer = new System.Threading.Timer(x =>
     {
         this.ShowMessageToUser();
     }, null, timeToGo, Timeout.InfiniteTimeSpan);
}

private void ShowMessageToUser()
{
    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(this.ShowMessageToUser));
    }
    else
    {
        MessageBox.Show("Your message");
    }
}
我问这个问题是因为我认为使用计时器不是最好的解决方案(定期比较当前时间和运行任务所需的时间)

为什么??为什么不是最好的解决方案呢?IMO定时器是最好的解决方案。但不是你实施的方式。尝试以下方法

private System.Threading.Timer timer;
private void SetUpTimer(TimeSpan alertTime)
{
     DateTime current = DateTime.Now;
     TimeSpan timeToGo = alertTime - current.TimeOfDay;
     if (timeToGo < TimeSpan.Zero)
     {
        return;//time already passed
     }
     this.timer = new System.Threading.Timer(x =>
     {
         this.ShowMessageToUser();
     }, null, timeToGo, Timeout.InfiniteTimeSpan);
}

private void ShowMessageToUser()
{
    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(this.ShowMessageToUser));
    }
    else
    {
        MessageBox.Show("Your message");
    }
}


您应该提供解决问题的代码示例。在下午5:45设置
计时器
。如果您误解了
计时器
类,再次查看文档可能是一个好主意。您应该提供解决问题的代码示例。在下午5:45设置
计时器
如果您误解了
计时器
类,再次查看文档可能是一个好主意。为什么要每分钟检查一次?我们不能将计时器设置为所需的时间吗?我认为最好使用tick事件处理程序创建一个间隔为1分钟(例如)的计时器,让程序检查每分钟的对应性。如果你想设置一个以上的“警报”,这会容易得多。我不这么认为,看看我的解决方案。我的意思是,如果你不想设置一个以上的警报,你将为每个警报创建一个计时器,另一方面,我承认,检查也会变得更重,你必须检查的事情越多,而不是每个警报一个计时器,我将设置第一个报警时间,当该事件过去时,我将为下一个间隔设置相同的计时器。这样我们可以重复使用计时器,避免每秒检查,为什么要检查每分钟?我们不能将计时器设置为所需的时间吗?我认为最好使用tick事件处理程序创建一个间隔为1分钟(例如)的计时器,让程序检查每分钟的对应性。如果你想设置一个以上的“警报”,这会容易得多。我不这么认为,看看我的解决方案。我的意思是,如果你不想设置一个以上的警报,你将为每个警报创建一个计时器,另一方面,我承认,检查也会变得更重,你必须检查的事情越多,而不是每个警报一个计时器,我将设置第一个报警时间,当该事件过去时,我将为下一个间隔设置相同的计时器。这样我们就可以重用计时器并避免在.NET4+中每秒检查时间:TimeSpan InfiniteTimeSpan=newtimespan(0,0,0,0,-1);NET 4+使用:TimeSpan InfinitieTimeSpan=新的TimeSpan(0,0,0,0,-1),而不是Timeout.InfinitieTimeSpan;而不是Timeout.InfiniteTimeSpan