我想让一个Windows Service 在某天的某个时间运行一次(一天运行一次),应该怎么做啊?

rockplayer 2006-07-06 12:58:50
我想让一个Windows Service 在某天的某个时间运行一次(一天运行一次),我加一个timer,然后在timer1_Elapsed方法中我是这样写的:

if(DateTime.Now.ToLongTimeString()=="10:49:55")
{
// do something...
}

服务也安装成功了,可以就是不运行里面的方法,是怎么回事啊??


=======================================================
我原来是这样写:
if(DateTime.Now.Hour==2) {
// do something...
}
这样就能运行,但是我发现这样不是每天运行一次,是每天运行很多次.


请教!!!
...全文
247 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
AIRFLYNET 2006-12-28
  • 打赏
  • 举报
回复
可以考虑Thread

using 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();
}
}
guodawu 2006-07-06
  • 打赏
  • 举报
回复
又可以学习了!Up
rockplayer 2006-07-06
  • 打赏
  • 举报
回复
谢谢 haidazi() 问题已解决.
rockplayer 2006-07-06
  • 打赏
  • 举报
回复
请教 计划任务 中运行的程序,在.NET中应该建成什么类型的啊?
我刚才建了一个Winform.结果在那个时间把运行的窗体跳出来了.有没有运行时没有什么显示的程序类型啊?
谢谢
rockplayer 2006-07-06
  • 打赏
  • 举报
回复
我不是想执行一段时间啊.,我是想在一天的某个时间点上执行一次/.就一次啊.
haidazi 2006-07-06
  • 打赏
  • 举报
回复
我想知道你的timer是隔多久运行一次,隔一秒运行一次才对的。
shalen520 2006-07-06
  • 打赏
  • 举报
回复
还不如做一个计划任务
fangwancong 2006-07-06
  • 打赏
  • 举报
回复
long now =DateTime.Now.Hour*60*60 + DateTime.Now.Minute*60 +DateTime.Now.Second ;
if ( now >= 你要执行时间总秒数 )
{
do something...
}
cm8983 2006-07-06
  • 打赏
  • 举报
回复
mark

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧