作息时间自动提醒Windows Service
经济危机真真切切的影响到了几乎每一个人,09年过去三分之一多一点了,生活不太规律,确实太忙的说,工作换了,房子也换了,又做回了张江男,觉得自己再不好好努力会Out的。。。都奔三儿而去了。。。于是乎,为了勉励自己,做了这么个东东。
一个Windows Service,里面跑一个Timer,每隔10秒check一下当前时间,一旦发现时间吻合,则调用Microsoft Speech Technology? 发声提醒,超级简单,不过。。。以后要么得一直戴着心爱的森海塞尔,要么只能用音箱了。。。
服务描述:
Notify Wayne to follow the schedule established by himself. If this service is disabled, Wayne may waste a lot of time...
项目层级:
- WayneScheduleService.cs Windows Service后台代码
- ScheduleItemsReader.cs Linq读取ScheduleItems
- SoundNotifier.cs 执行发声提醒
- ServiceLogManager.cs 打Log
主要程序代码:
调用程序发声的代码:
public static class SoundNotifier
{
public static void Phonate(String sentence)
{
SpeechSynthesizer speaker = new SpeechSynthesizer();
CultureInfo cnCulture = CultureInfo.GetCultureInfo("en-US");
PromptBuilder pb = new PromptBuilder(CultureInfo.CurrentCulture);
pb.StartVoice(VoiceGender.Female, VoiceAge.Adult);
pb.StartStyle(new PromptStyle(PromptVolume.ExtraLoud));
pb.StartSentence();
pb.AppendText(sentence, PromptEmphasis.Strong);
pb.EndSentence();
pb.EndStyle();
pb.EndVoice();
speaker.Speak(pb);
}
}
晚间作息时间弄在XML里,根据情况自行配置(搞烦了全删掉。。。):
<?xml version="1.0" encoding="utf-8" ?>
<WayneScheduleItems>
<ScheduleItem TriggerHour="18" TriggerMinute="20" Content="Hi! Wayne! Dinner Time!" />
<ScheduleItem TriggerHour="19" TriggerMinute="0" Content="Hi! Wayne! It is time to learn technologies and programming skills!" />
<ScheduleItem TriggerHour="20" TriggerMinute="30" Content="Hi! Wayne! Take a rest, entertainment, gaming, exercise and wash." />
<ScheduleItem TriggerHour="22" TriggerMinute="0" Content="Well, read/write Emails, read some news." />
<ScheduleItem TriggerHour="23" TriggerMinute="0" Content="Wayne, go to bed, sleep! Good night!" />
</WayneScheduleItems>
使用Linq读取XML里的ScheduleItems:
List<ScheduleItem> scheduleItems = new List<ScheduleItem>();
IEnumerable<XElement> scheduleItemsCollection = from scheduleItem in XElement.Load(ScheduleItemsXmlLocation).Descendants("ScheduleItem") select scheduleItem;
foreach (XElement ele in scheduleItemsCollection)
{
ScheduleItem scheduleItem = new ScheduleItem();
scheduleItem.TriggerHour = ushort.Parse(ele.Attribute("TriggerHour").Value);
scheduleItem.TriggerMinute = ushort.Parse(ele.Attribute("TriggerMinute").Value);
scheduleItem.Content = ele.Attribute("Content").Value;
scheduleItems.Add(scheduleItem);
}
return scheduleItems;
最后是Timer:
const double WorkInterval = 10000d;
m_Timer = new Timer(WorkInterval);
m_Timer.Enabled = true;
m_Timer.Elapsed += new ElapsedEventHandler(OnTimerElapse);
源代码下载
Note: 由于使用Linq的缘故只能运行于Donet 3.5,由于使用Microsoft Speech Technology? ,仅支持XP以上操作系统(在XP中还有点问题,比如不支持女声,并且不支持中文)。
Leave a comment