.net - 为什么此计时器不会在 .net 服务应用程序中启动?

标签 .net vb.net windows-services timer

我有我在 .NET 中编写的 Windows 服务的这段代码......但是,无论我在 tmrRun 属性中放置的时间间隔如何,TICK 函数都不会被执行。我错过了什么?我确信它是我没有看到的愚蠢的东西。

谢谢你
Imports System.IO Public Class HealthMonitor Protected Overrides Sub OnStart(ByVal args() As String) // Add code here to start your service. This method should set things // in motion so your service can do its work. tmrRun.Start() End Sub Protected Overrides Sub OnStop() // Add code here to perform any tear-down necessary to stop your service. tmrRun.Stop() End Sub Private Sub tmrRun_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrRun.Tick //DO some stuff End Sub End Class

最佳答案

这对我有帮助。

进口系统
导入 System.Timers

公开课 MyTimers

Protected Overrides Sub OnStart(ByVal args() As String)

Dim MyTimer 作为新 System.Timers.Timer()
AddHandler MyTimer.Elapsed, AddressOf OnTimedEvent

' 将间隔设置为 10 秒(10000 毫秒)。
MyTimer.Interval = 10000
MyTimer.Enabled = True
结束子

Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
结束子
结束类

关于.net - 为什么此计时器不会在 .net 服务应用程序中启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/983540/

相关文章:

C# 属性访问优化

c# - 在 C# 中,引用数组变量是否更慢?

c# - Range.Information 不工作 C#

c# - BackgroundWorker 和 System.Threading.Thread 之间的区别

vb.net - 尝试多种捕获行为

c# - 在 C# .NET 中创建 Windows 服务的最佳指南?

.net - Resharper 是否不支持 Microsoft 命名空间命名约定最佳实践?

c# - 如果第一个代码正在执行,如何等待?

tomcat - 在services.msc中安装tomcat服务

windows - 将 GHC 创建的 Windows 可执行文件作为服务运行