0
我在VB.NET服務中有System.Threading.Timer
。但我不確定爲什麼計時器只運行了14次然後停止。該服務仍然保持運行。下面是代碼:System.Threading.Timer將在幾次後停止
Protected Overrides Sub OnStart(ByVal args() As String)
Dim downloadCall As New TimerCallback(AddressOf download_tick)
Dim timerDownload As New Timer(downloadCall, Nothing, 0, 8000)
End Sub
Private Sub download_tick(ByVal sender As Object)
System.IO.File.AppendAllText(My.Application.Info.DirectoryPath & "\test\" & Now.ToString("yyyyMMddHHmmss") & ".txt", "")
Dim th As New Thread(AddressOf downloadPrintJob)
th.Start()
End Sub
Private Sub downloadPrintJob()
Try
Using webClient As New System.Net.WebClient()
.....
從上面的代碼,我發現有產生14文本文件,每一次,如果我重新啓動該服務。
呃,我看到的一件事就是''在'OnStart'中調暗你的計時器,然後在'OnStart'完成時刪除對它的引用。 GC可能會吞嚥它,儘管我不認爲它會像每次運行它時在14次滴答後處理它一樣確定性。另外,如果兩次下載同時完成並將它們追加到同一文件(如果秒數相同)呢? – helrich
感謝helrich,你是對的,我把定時器聲明移到了onstart之外,然後它就可以工作。對於同時完成的2次下載,它確實會拋出一個異常,「文本文件正在被其他進程使用」。 –
酷!很高興爲你工作。 – helrich