在单个Windows服务中运行两个System.threading.timers

时间:2014-07-07 14:07:42

标签: vb.net timer

我需要运行两个单独的System.threading.timer个实例,并且我已经设置了我的服务以便以这种方式运行,但是第二个计时器并未启动。第一个继续正常射击。这个我表示我称之为两个计时器:

Dim timerDelegate As TimerCallback = AddressOf tick
Dim autoEvent As New AutoResetEvent(True)
Dim milliseconds As Integer = My.Settings.second * 1000 + 60 * 1000 *       My.Settings.minute + 60 * 60 * 1000 * My.Settings.hour        
Dim dt As New System.Threading.Timer(timerDelegate, autoEvent, 0, milliseconds)

Dim timerDelegate2 As TimerCallback = AddressOf tick2              
Dim autoEvent2 As New AutoResetEvent(True)    
Dim milliseconds2 As Integer = 10 * 1000 * My.Settings.time    
Dim dt2 As New System.Threading.Timer(timerDelegate, autoEvent, 0, milliseconds)

这是第一个计时器的代码

Private Sub tick(ByVal stateInfo As Object) ' to delete the clt files    
   log.message("inside this one")   
   Dim files As String()   
   files = IO.Directory.GetFiles(sourcefile, "*")   
   Dim file As String   
   Dim r2 As Regex = New Regex(".*\.clt|.*\.CLT")   
   For Each file In files   
       Dim mathcer2 As Match = r2.Match(file) 'match for clt   
       If mathcer2.Success Then    
           System.IO.File.Delete(file)   

       End If   
    Next   
End Sub  

第二个计时器的代码大致相同。

有人知道为什么会这样吗? 编

1 个答案:

答案 0 :(得分:1)

您正在为两个计时器使用相同的委托实例:

Dim dt2 As New System.Threading.Timer(timerDelegate, autoEvent, 0, milliseconds

应该是:

Dim dt2 As New System.Threading.Timer(timerDelegate2, autoEvent, 0, milliseconds

您也使用相同的AutoResetEvent对象。我不清楚你在做什么,但似乎你已经复制/粘贴了太多。