微软对此在这两篇文章那个中给出了相应的解决方案,http://support.microsoft.com/kb/820639/zh-cn,http://support.microsoft.com/kb/842793/zh-cn。
实际上就是在使用Windows.Forms命名空间中的timer无效时则使用System.Timers中的组件,再不行的话使用Threading中的Timer组件。我遇到的是第一种情况,通过使用System.Timers中的Timer,问题解决了。
以下是本人编写的一个例子:
Program.cs
ProjectInstaller.cs
ProjectInstaller.Designer.cs
Service1.cs
Service1.Designer.cs
其中,POPPersistent类及其方法OperationEmail()是要关联的程序。
实际上就是在使用Windows.Forms命名空间中的timer无效时则使用System.Timers中的组件,再不行的话使用Threading中的Timer组件。我遇到的是第一种情况,通过使用System.Timers中的Timer,问题解决了。
症状
您添加到 Windows 服务 Microsoft WindowsForms 计时器组件。 启用计时器, 然后您设置计时器以引发事件的间隔。 计算机, 上安装 Windows 服务, 然后您启动服务。 不引发 计时器 事件在 Windows 服务。
注意 : WindowsForms 计时器组件位于 System.Windows.Forms 命名空间中。
注意 : WindowsForms 计时器组件位于 System.Windows.Forms 命名空间中。
原因
WindowsForms 计时器组件用于 WindowsForms 环境,不用于服务器环境。 计时器如果在 Windows 服务使用时, 可能不引发事件。
解决方案
要解决此问题, 使用服务器计时器时让命名空间 System.Timers 代替 System.Windows.Forms 命名空间。 要这样做, 请按照下列步骤操作:
1. | 在要删除 Service 1 类命令窗口运行以下命令:
installutil /u WindowsService1.exe
注意 WindowsService1.exe 位于您的项目文件夹下 bin 文件夹中。 添加路径是您 WindowsService1.exe。 |
2. | 切换到 Windows 服务项目。 |
3. | 在 Service 1 设计器窗口, 单击 Timer 1 控件。 |
4. | 按 DELETE 键从设计器窗口中删除该 Timer 1 控件。 |
5. | 在工具栏上, 单击 组件 选项卡。 |
6. | 从工具栏, 将 Timer 控件拖到设计器窗口。 |
7. | 双击设计器窗口以查看代码窗口上 Timer 1 控件。 |
8. | Timer 1 控件的 Tick 事件处理程序中移动到的 Timer 1 控件 Elapsed 事件处理程序代码 |
9. | 移除 Tick 事件处理程序。 |
10. | 在 生成 菜单上, 单击 BuildSolution@@@ 。 |
11. | 在命令窗口运行以下命令:
installutil WindowsService1.exe
|
12. | 在 管理工具 菜单上, 单击 服务 。 |
13. | 右击 Service 1 , 然后单击 开始 。 |
14. | 等待几秒钟, 右击 Service 1 , 然后单击 停止 。 |
15. | 打开, C:\sample.txt 文件, 然后注意文本。 以 开始 和 停止 Tick 文本显示。 |
状态
此行为是设计使然。
更多信息
再现问题的步骤
1. | 启动 MicrosoftVisualStudio.NET。 |
2. | 通过使用 MicrosoftVisualBasic.NET 或 MicrosoftVisualC # .NET 打开一个新 Windows 服务项目。 默认情况下, 创建 Service 1 。 |
3. | 在工具箱中, 单击 WindowsForms 选项卡。 |
4. | 将一个 Timer 控件从工具箱拖到 Service 1 设计器窗口。 |
5. | 在设计器窗口, 双击 Timer 1 控件。 显示 Service 1 代码窗口。 |
6. | 如果您使用 VisualC # .NET, 将以下语句添加到开头的代码:
|
7. | 以下代码添加到 Service 1 类的 OnStart 过程。 VisualBasic.NET 代码
VisualC # .NET 代码
|
8. | 以下代码添加到 Service 1 类的 OnStop 过程。 VisualBasic.NET 代码
VisualC # .NET 代码
|
9. | 将以下代码添加到 Timer 1 组件的 Tick 事件。 VisualBasic.NET 代码
VisualC # .NET 代码
|
10. | 在 视图 菜单上, 单击 设计器 。 |
11. | 右击 设计器 , 然后单击 添加 Installer 。 默认情况下, ServiceInstaller 1 和 ServiceProcessInstaller1 创建。 |
12. | ServiceInstaller 1 , 右击, 然后单击 属性 。 |
13. | 将 DisplayName 属性设置为 Service 1 。 注意 最好要将 ServiceName 属性设为 Service 1 。 |
14. | 右击 ServiceProcessInstaller1 , 然后单击 属性 。 |
15. | 在属性窗口, ServiceProcessInstaller1 将以 LocalSystem 帐户 属性。 |
16. | 在 生成 菜单上, 单击 BuildSolution@@@ 。 |
17. | 开始 , 依次 运行 。 |
18. | 在运行窗口, 在 打开 框中, 键入 cmd , 然后按 ENTER 键。 |
19. | 在命令提示符下, 运行以下命令:
注意 如果使用任何其他服务具有相同名称不安装您 Service 1 服务。 |
20. | 完成安装步骤后, AdministrativeTools , 控制面板中单击, 然后单击 服务 。 |
21. | 在服务窗口, 右击 Service 1 , 依次 开始 。 |
22. | 等待几秒钟, 右击 Service 1 , 然后单击 停止 。 |
23. | 打开 C:\sample.txt 文件。 显示文本 StartStop 。 不引发 Timer 1 Tick 事件, 并且不打印到 C:\sample.txt 文件 Tick 文本。 |
以下是本人编写的一个例子:
Program.cs
Code
ProjectInstaller.cs
Code
ProjectInstaller.Designer.cs
Code
Service1.cs
Code
Service1.Designer.cs
Code
其中,POPPersistent类及其方法OperationEmail()是要关联的程序。