如何在Windows服务中使用事件处理程序 [英] How to use event handler in windows service

查看:75
本文介绍了如何在Windows服务中使用事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生/小姐,



我是Windows服务的新手,我一直在开发用于检测系统日期变化的新Windows服务。

一旦系统日期发生变化,那么Windows服务就应该写日志。



我写了以下代码,

Dear sir/s,

I am new in windows services, i have been developing new windows services for detecting system date changes.
as soon as system date changes then windows services should write log.

for that i have written following code,

 Protected Overrides Sub OnStart(ByVal args() As String)
   AddHandler SystemEvents.TimeChanged, AddressOf SystemEvents_TimeChanged
End Sub

Private Sub SystemEvents_TimeChanged(ByVal sender As Object, ByVal e As EventArgs)
    WriteToFile("System Time has been changed")
End Sub





但是当我更改系统日期时,Windows服务不会触发事件。

请帮帮我。

谢谢。



But when i change system date then windows service does not fire above event.
Please help me.
Thank you.

推荐答案

阅读有关文档的备注 [<小时ef =https://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.timechanged%28v=vs.110%29.aspx\"target =_ blanktitle =New Window> ^ ]:

Read the remarks on the documentation[^]:



注意

只有在消息泵运行时才会引发此事件。在Windows服务中,除非使用隐藏表单或手动启动消息泵,否则不会引发此事件。有关演示如何使用Windows服务中的隐藏表单处理系统事件的代码示例,请参阅 SystemEvents [ ^ ]。


Note
This event is only raised if the message pump is running. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. For a code example that shows how to handle system events by using a hidden form in a Windows service, see the SystemEvents class[^].


关于 <$的第二个例子c $ c> SystemEvents class [ ^ ]说明了如何使其工作。


The second example on the SystemEvents class[^] explains what you need to do to make this work.


这篇关于如何在Windows服务中使用事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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