C#事件来检测夏令甚至手动时间的变化 [英] C# event to detect Daylight Saving or even manual time change

查看:148
本文介绍了C#事件来检测夏令甚至手动时间的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个应用程序服务器上的服务,然后我有多个客户端连接到该服务。我显示在每个客户端窗口窗体应用程序的准确服务器时间正因为如此,我需要一个机制来检测服务器上的任何时间的变化,这样我可以将它发送给客户端显示。

I run an application as a service on a server and then I have multiple clients connect to that service. I display the exact server time on each of the client Window form application and because of that I need a mechanism to detect any time change on the server so that I can send it to the client to display.

看在MSDN,我发现SystemEvents.TimeChanged,我以为我很幸运,因为下面的Visual Studio一切code测试时的伟大工程:

Looking over the MSDN I found SystemEvents.TimeChanged , I thought I'm in luck because when testing with the code below with Visual Studio everything works great:

SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);

static void SystemEvents_TimeChanged(object sender, EventArgs e)
    {
        //Occurs when user manually changes the time or due to
        //daylight saving time.  Server will issue its latest time
        //to all the clients so time can be adjusted.
    }

不幸的是,当我运行此作为一个服务,这个事件将永远不会被解雇,因为在看MSDN上的小字,在TimeChanged事件只能与一个消息泵的应用火灾(又名它必须有一个正在运行的形式)活跃。它的工作原理在Visual Studio中,因为它有一个形式,当我运行它作为非服务模式。

Unfortunately when I run this as a service, this event will never get fired because looking at the fine print on MSDN, the TimeChanged event only fires in an application with a message pump (aka it has to have a running form) active. It works in Visual Studio because it has a form when I run it as a non service mode.

我可以通过检查选项允许服务与桌面交互,但那种失败我的目的试图运行的应用程序作为服务的纠正。

I can correct this by checking the option "Allow service to interact with desktop" but that kind of defeats the purpose of me trying to run the application as a service.

我可以在应用程序中创建一个计时器滴答事件来检查时间的变化,让我们每10秒或者这么说,但是这是一个很大的开销只是为了检查该服务器上出现了两次或者3的随机时间的变化每年次。

I could create a timer tick event in the application to check for time change, let's say every 10 seconds or so but this is a big overhead just to check for a random time change that occurs on the server machine twice or maybe 3 times per year.

因此​​,任何人知道,在时间的变化触发一个C#的事件,在不具有消息泵将一个应用程序的工作原理?我很惊讶,如果微软不包括以这种方式工作的事件。

So anyone know a C# event that trigger on time change that works in an application that does not have message pump going? I'm surprise if Microsoft does not include an event that works this way

感谢您。

推荐答案

感谢大家的投入,他们肯定我指出了正确的方向。我能解决我的问题,我想将它们放在这里任何人失望的发现他们有帮助的道路。

Thank you all for your inputs, they certainly pointed me in the right direction. I was able to solve my issue and I figure to put them here for anyone down the road that find them helpful.

所以在我的主程序,我运行的服务,它必须是一个子类ServiceBase的。

So in my main program where I run the service, it has to be a sub class of ServiceBase.

在我的主要功能I催生了一个单独的线程如下:

In my main function I spawned a separate thread as follow:

new Thread(RunMessagePump).Start();

private void RunMessagePump()
{
  Application.Run(new HiddenForm())
}

这隐藏的表单也正是它的名字所说的,它是一个简单的形式,仍然受到在其Intiailaizecomponent()code中的code this.ResumeLayout(假)是隐藏的。

This Hidden form is exactly what its name say, it is a simple form that is remain hidden by the code this.ResumeLayout(false) under its Intiailaizecomponent() code.

在这个表单的FormLoad事件时,可以声明任何系统事件。在我来说,我有

Within this form's FormLoad event you can declare any System event. In my case I have

private void HiddenForm_Load(object sender,EventArgs e)
{
  SystemEvents.timeChanged += new EventHandler(SystemEvents_TimeChanged);
}

瞧,这个精美的作品出来,我想我会用这种形式加载事件捕捉我想用在道路上的任何SystemEvents。

Voila, this works out beautifully, and I think I will use this form load event to capture any SystemEvents I want to use down the road.

干杯。

这篇关于C#事件来检测夏令甚至手动时间的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆