如何在C#写的WINDOWS服务中,使线程在固定的时间运行?

haidylau 2008-03-11 05:21:55
加精
只存在一个线程,这个线程希望每隔几个小时执行一次,例如每天的1点,5点,9点,13点,17点,21点!
在不论什么时间启动该服务,只要它不在这几个时间上,就不让它运行!!
想问问有没有什么好的方法?
我想到一个是在该线程start之前,先获得当前时间的HOUR,如果不在这几个的话,再一一判断,当前时间距以上以个时间断有多长时间,然后让线程sleep这些时间
不过我觉得这个办法太笨,有没有更好的方法呢?
...全文
1996 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
qufusongyu 2009-04-06
  • 打赏
  • 举报
回复
http://www.msdnwebcast.com/msdn/webcast.aspx?query=1949#430056548
qche111 2009-03-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 xray2005 的回复:]
感觉 既然是windows服务,如果让它开就自动运行,那系统服务本来就是一直在运行,直接while(true)就可以了.
不需要什么timer或sleep了.

在start里写

while(true)
{
switch(当前时间)//判断当前时间是否是1点,5点,9点,13点,17点,21点

{
case 1点:
//执行你的代码
//....其他几点类似就行了.
}

}
[/Quote]

你这样太耗CPU资源了吧。
ymcscu 2008-03-21
  • 打赏
  • 举报
回复
用timer不行么?

间隔设置成1秒,每次触发时判断当前时间是否是指定时间
nbkyo 2008-03-17
  • 打赏
  • 举报
回复
这是我以前写的,希望对你有帮助。
呵呵。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;

namespace TickService
{
public partial class Service1 : ServiceBase
{
Timer timer1;

public Service1()
{
InitializeComponent();
timer1 = new Timer();
//Set the interval of timer to 3 seconds.
timer1.Interval = 3000;
//Enable the timer.
timer1.Enabled = true;
timer1.Elapsed+=new ElapsedEventHandler(timer1_Elapsed);
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.

//Append the text to the sample file.
StreamWriter writer = File.AppendText(@"C:\sample.txt");
writer.WriteLine("Start");
writer.Close();
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
//Append the text Stop to the C:\sample.txt file.
StreamWriter writer = File.AppendText(@"C:\sample.txt");
writer.WriteLine("Stop");
writer.Close();
}

private void timer1_Elapsed(object sender, EventArgs e)
{
//Set the enable property to false.
//timer1.Enabled = false;
//Append the text Tick to the C:\sample.txt file.
StreamWriter writer = File.AppendText(@"C:\sample.txt");
writer.WriteLine("Tick");
writer.Close();
//timer1.Enabled = true;
}
}
}
fuda_1985 2008-03-17
  • 打赏
  • 举报
回复
mark
chxljtt 2008-03-17
  • 打赏
  • 举报
回复
關注一下,現在正在找尋這方面的資料!
soaringbird 2008-03-16
  • 打赏
  • 举报
回复
如果不要求太精确,可以在线程循环里判断是否在指定的时间前后的一个范围呢,如果不在就每次循环休眠一分钟。
如果要求时间比较精确,休眠的间隔就要比较短,但太短就会占用太多的CPU时间,那么可以用折半逼近指定的时间范围。
xray2005 2008-03-16
  • 打赏
  • 举报
回复
感觉 既然是windows服务,如果让它开就自动运行,那系统服务本来就是一直在运行,直接while(true)就可以了.
不需要什么timer或sleep了.

在start里写

while(true)
{
switch(当前时间)//判断当前时间是否是1点,5点,9点,13点,17点,21点

{
case 1点:
//执行你的代码
//....其他几点类似就行了.
}

}
yuanmanguo 2008-03-16
  • 打赏
  • 举报
回复
mark
icwin 2008-03-16
  • 打赏
  • 举报
回复
mark
like01_12 2008-03-16
  • 打赏
  • 举报
回复
学习~~
hertcloud 2008-03-12
  • 打赏
  • 举报
回复
这个是以前写的一个服务中 用过的多线程
应该有帮助
http://blog.csdn.net/hertcloud/archive/2007/04/07/1556112.aspx
品铭工作室 2008-03-11
  • 打赏
  • 举报
回复
无任是根据时间启动服务运行你的程序还在服务中根据时间启动你的程序,都要在线程中的一个循环中实现,如果你明白的话,前都是没有意义的,之前做的一个项目这有一个功能和你所说的差不多,我先做了一个服务,里面什么都没有写,只引用了一个DLL,并在这个DLL中预留了一个服务要启动程序的入口点的方法,通过这个方法引用我要实现的业务功能对象,这样只要更新这个DLL文件,服务也会同时更新
shinaterry 2008-03-11
  • 打赏
  • 举报
回复
^ō^ Timer | BackgroundWorker 已经足够...
骑蚊子旅游 2008-03-11
  • 打赏
  • 举报
回复
呵呵。学习。。
KingReturns 2008-03-11
  • 打赏
  • 举报
回复
用timer可以吧
Ivony 2008-03-11
  • 打赏
  • 举报
回复
Sleep一分钟,醒来的时候看看到点没,没有就继续Sleep一分钟。

没有必要追求极致性能,每一分钟才执行一次操作的程序的开销用户根本感觉不出来。
haidylau 2008-03-11
  • 打赏
  • 举报
回复
真的没有更加简单的办法了吗?
cpio 2008-03-11
  • 打赏
  • 举报
回复

可以这样啊,反正Sleep也基本不占用资源

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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