我的系统中需要每天零时促发一个事件来做一些操作,请问如何在C#下用有关日期的操作来实现这个事件,即如何让运行中的程序自动判断现在的系统时间已经到零时(或任意一个时刻)了,应该做相应操作了。

解决方案 »

  1.   

    数据库操作还是什么操作啊?如果是数据库操作写job如果是程序操作,只是判断时间问题,但是你得保证你的程序在运行,或者写成winserviceSystem.DateTime.Now.ToShortTimeString()
      

  2.   

    确实需要数据库和程序都要操作,我想在程序中能触发一个事件,用代码进行数据库的操作,而且不想写成winservice的形式,还望您指点。具体该如何做?
      

  3.   

    System.DateTime.Now.ToShortTimeString()是用using加进去嘛??
      

  4.   

    System.DateTime.Now.ToShortTimeString() 是获取当前时间的短类型   
    即2005-5-12   而不是2005年5月12日
      

  5.   

    public class Timer1
     {
     
         public static void Main()
         {
             System.Timers.Timer aTimer = new System.Timers.Timer();
             aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
             // Set the Interval to 5 seconds.
             aTimer.Interval=5000;
             aTimer.Enabled=true;
     
             Console.WriteLine("Press \'q\' to quit the sample.");
             while(Console.Read()!='q');
         }
     
         // Specify what you want to happen when the Elapsed event is raised.
         public static void OnTimedEvent(object source, ElapsedEventArgs e)
         {
             Console.WriteLine("Hello World!");
         }
     }MSDN中这段代码会和主程序入口的Main()冲突。Timer2 a;
    a.Interval=5000;
    这两条语句又无效,请问Timer类如何使用,现成的例子不能使用啊,命名空间已经加上了,希望有人能给一个使用Elapsed方法和Interval属性进行定时触发的例子。谢谢了。
      

  6.   

    有人说可以调用Windows的计划任务的API来实现,但我查不到具体怎么做,有没有其他解决的办法?
      

  7.   

    你在form中放一个timer,设置好属性,然后双击他,就会生成代码,然后你可以在事件里写程序判断时间是否符合要求
    但是这样做你就得保证这个程序一直在运行,这样太浪费资源了api的方法没用过,你可以找api相关的例子,看看里面的函数是怎样写的,应该可以做到
      

  8.   

    窗口设置Timer控件:
    构造函数代码如下:
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    timer1.Tick += new EventHandler(TimerEventProcessor);
     
    // Sets the timer interval to 5 seconds.
    timer1.Interval = 1000;
    timer1.Start();}相应事件代码:
    // This is the method to run when the timer is raised.
    private void TimerEventProcessor(Object myObject,
    EventArgs myEventArgs) 
    {
    if(System.DateTime.Now.Hour == 15)
    {
    tb.Text = System.DateTime.Now.ToString("yyyy-MM-dd yy:mm");
    }
    }