我想让一个Windows Service 在某天的某个时间运行一次(一天运行一次),我加一个timer,然后在timer1_Elapsed方法中我是这样写的: if(DateTime.Now.ToLongTimeString()=="10:49:55") 
{
    // do something...
}服务也安装成功了,可以就是不运行里面的方法,是怎么回事啊??
=======================================================
我原来是这样写:
if(DateTime.Now.Hour==2) {
    // do something...
}
这样就能运行,但是我发现这样不是每天运行一次,是每天运行很多次.
请教!!!

解决方案 »

  1.   

    long now =DateTime.Now.Hour*60*60 + DateTime.Now.Minute*60 +DateTime.Now.Second ;
    if ( now >= 你要执行时间总秒数 )
    {
       do something...
    }
      

  2.   

    我想知道你的timer是隔多久运行一次,隔一秒运行一次才对的。
      

  3.   

    请教 计划任务 中运行的程序,在.NET中应该建成什么类型的啊?
    我刚才建了一个Winform.结果在那个时间把运行的窗体跳出来了.有没有运行时没有什么显示的程序类型啊?
    谢谢
      

  4.   

    谢谢 haidazi()  问题已解决.
      

  5.   

    可以考虑Threadusing System;
    using System.Threading;// Simple threading scenario:  Start a static method running
    // on a second thread.
    public class ThreadExample {
        // The ThreadProc method is called when the thread starts.
        // It loops ten times, writing to the console and yielding 
        // the rest of its time slice each time, and then ends.
        public static void ThreadProc() {
            for (int i = 0; i < 10; i++) {
                Console.WriteLine("ThreadProc: {0}", i);
                // Yield the rest of the time slice.
                Thread.Sleep(0);
            }
        }    public static void Main() {
            Console.WriteLine("Main thread: Start a second thread.");
            // The constructor for the Thread class requires a ThreadStart 
            // delegate that represents the method to be executed on the 
            // thread.  C# simplifies the creation of this delegate.
            Thread t = new Thread(new ThreadStart(ThreadProc));
            // Start ThreadProc.  On a uniprocessor, the thread does not get 
            // any processor time until the main thread yields.  Uncomment 
            // the Thread.Sleep that follows t.Start() to see the difference.
            t.Start();
            //Thread.Sleep(0);        for (int i = 0; i < 4; i++) {
                Console.WriteLine("Main thread: Do some work.");
                Thread.Sleep(0);
            }        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
            t.Join();
            Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
            Console.ReadLine();
        }
    }