|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Windows Service, timer doesn't tickSystem.Timers.Timer instead for the one for forms. So that's what I do, but still it doesn't trigger: '--code begin Imports System.Net Imports System.Net.Sockets Imports System.Net.Dns Imports System.Text Imports System.IO Public Class Service1 Private bTrigger As Boolean Private myTimer As New System.Timers.Timer Protected Overrides Sub OnStart(ByVal args() As String) With myTimer .Interval = 10000 .Enabled = True .Start() End With LogFile(Now & ": " & vbTab & "Service started") End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. LogFile(Now & ": " & vbTab & "Service stopped") End Sub Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) LogFile(Now & ": " & vbTab & "Timer trigger - do nothing") If Minute(Now) = 33 Then LogFile(Now & ": " & vbTab & "Timer trigger - run code") If bTrigger = False Then ExecuteWOL() End If End Sub .... '---code end So, the file should be written to every 10 seconds, but it doesn't. The file is correctly written to upon start/stop of service. Any ideas? Regards /Snedker I would abondon the timer method and use
System.Threading.Thread.Sleep(10000) on a do loop. If you use this method you'll need to create a new thread and put your code is another sub, not in OnStart. Something like this: ****************************************************************************** Public Class Service1 Private thWorker As New System.Threading.Thread(AddressOf DoSomething) Private bTrigger As Boolean = False Protected Overrides Sub OnStart(ByVal args() As String) LogFile(Now & ": " & vbTab & "Service started") thWorker.Start() End Sub Protected Overrides Sub OnStop() LogFile(Now & ": " & vbTab & "Service stopped") thWorker.Abort() End Sub Private Sub DoSomething() Do thWorker.Sleep(10000) LogFile(Now & ": " & vbTab & "Timer trigger - do nothing") If Minute(Now) = 33 Then LogFile(Now & ": " & vbTab & "Timer trigger - run code") If bTrigger = False Then ExecuteWOL() End If Loop End Sub End Class ***************************************************************************** Izzy Morten Snedker wrote: Show quoteHide quote > Others had the same problem and I understand that I have to use the > System.Timers.Timer instead for the one for forms. So that's what I > do, but still it doesn't trigger: > > '--code begin > Imports System.Net > Imports System.Net.Sockets > Imports System.Net.Dns > Imports System.Text > Imports System.IO > Public Class Service1 > > Private bTrigger As Boolean > Private myTimer As New System.Timers.Timer > > Protected Overrides Sub OnStart(ByVal args() As String) > > With myTimer > .Interval = 10000 > .Enabled = True > .Start() > End With > > LogFile(Now & ": " & vbTab & "Service started") > > End Sub > > Protected Overrides Sub OnStop() > ' Add code here to perform any tear-down necessary to stop > your service. > LogFile(Now & ": " & vbTab & "Service stopped") > End Sub > > Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As > System.EventArgs) > > LogFile(Now & ": " & vbTab & "Timer trigger - do nothing") > > If Minute(Now) = 33 Then > LogFile(Now & ": " & vbTab & "Timer trigger - run code") > If bTrigger = False Then ExecuteWOL() > End If > > End Sub > ... > '---code end > > So, the file should be written to every 10 seconds, but it doesn't. > The file is correctly written to upon start/stop of service. > > Any ideas? > > Regards /Snedker On 25 Sep 2006 06:48:40 -0700, "Izzy" <israel.rich***@gmail.com> Thanks for your response that put me on the right track. The workingwrote: solution: Public Class Service1 Protected Overrides Sub OnStart(ByVal args() As String) Dim thWorker As New Thread(AddressOf WorkProcess) LogFile(Now & ": " & vbTab & "Service started") thWorker.Start() End Sub Protected Overrides Sub OnStop() LogFile(Now & ": " & vbTab & "Service stopped") End Sub Sub WorkProcess() Do Thread.Sleep(60000) If Minute(Now) = 44 Then ExecuteWOL() Loop End Sub .... Thx again. Regards /Snedker There needs to be a thWorker.Abort() in your OnStop() method.
Izzy Morten Snedker wrote: Show quoteHide quote > On 25 Sep 2006 06:48:40 -0700, "Izzy" <israel.rich***@gmail.com> > wrote: > > Thanks for your response that put me on the right track. The working > solution: > > Public Class Service1 > > Protected Overrides Sub OnStart(ByVal args() As String) > > Dim thWorker As New Thread(AddressOf WorkProcess) > > LogFile(Now & ": " & vbTab & "Service started") > thWorker.Start() > > End Sub > > Protected Overrides Sub OnStop() > LogFile(Now & ": " & vbTab & "Service stopped") > End Sub > Sub WorkProcess() > > Do > Thread.Sleep(60000) > If Minute(Now) = 44 Then ExecuteWOL() > Loop > > End Sub > ... > > > Thx again. > > > Regards /Snedker Izzy wrote:
> There needs to be a thWorker.Abort() in your OnStop() method. You should not abort a thread in this manner. You should send some> signal into the thread so it can shutdown in an orderly fashion. Chris Dunaway wrote:
> Izzy wrote: Can you give a code example of how to properly shut down a thread?> > There needs to be a thWorker.Abort() in your OnStop() method. > > > > You should not abort a thread in this manner. You should send some > signal into the thread so it can shutdown in an orderly fashion. Izzy Hi Morten,
Maybe I've missed something in the code but it looks like you haven't got either an AddHandler or Handles clause for the myTimer_Tick event. This could be the problem. e.g. Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick PGC Show quoteHide quote "Morten Snedker" <morten_spammenot_ATdbconsult.dk> wrote in message news:g3ifh259cmmuhln3gp0nsanqm20ijoribb@4ax.com... > Others had the same problem and I understand that I have to use the > System.Timers.Timer instead for the one for forms. So that's what I > do, but still it doesn't trigger: > > '--code begin > Imports System.Net > Imports System.Net.Sockets > Imports System.Net.Dns > Imports System.Text > Imports System.IO > Public Class Service1 > > Private bTrigger As Boolean > Private myTimer As New System.Timers.Timer > > Protected Overrides Sub OnStart(ByVal args() As String) > > With myTimer > .Interval = 10000 > .Enabled = True > .Start() > End With > > LogFile(Now & ": " & vbTab & "Service started") > > End Sub > > Protected Overrides Sub OnStop() > ' Add code here to perform any tear-down necessary to stop > your service. > LogFile(Now & ": " & vbTab & "Service stopped") > End Sub > > Private Sub myTimer_Tick(ByVal sender As System.Object, ByVal e As > System.EventArgs) > > LogFile(Now & ": " & vbTab & "Timer trigger - do nothing") > > If Minute(Now) = 33 Then > LogFile(Now & ": " & vbTab & "Timer trigger - run code") > If bTrigger = False Then ExecuteWOL() > End If > > End Sub > ... > '---code end > > So, the file should be written to every 10 seconds, but it doesn't. > The file is correctly written to upon start/stop of service. > > Any ideas? > > Regards /Snedker Morten Snedker wrote:
Show quoteHide quote > Others had the same problem and I understand that I have to use the What does the LogFile method do? Do you see the message that says> System.Timers.Timer instead for the one for forms. So that's what I > do, but still it doesn't trigger: > > '--code begin > Imports System.Net > Imports System.Net.Sockets > Imports System.Net.Dns > Imports System.Text > Imports System.IO > Public Class Service1 > > Private bTrigger As Boolean > Private myTimer As New System.Timers.Timer > > Protected Overrides Sub OnStart(ByVal args() As String) > > With myTimer > .Interval = 10000 > .Enabled = True > .Start() > End With > > LogFile(Now & ": " & vbTab & "Service started") > > End Sub "Service Started" ? Does the service have the necessary permissions to write to the log file? <snip>
Bubbling a status message to the UI
Bubbling an Exception to the UI Any examples of this floating around? Difference between CDec and CDbl ComboBox Help with some research multi line list box Getting A generic error occurred in GDI+ message ComboBox-binding an ein gefiltertes Dataset.Table-Object VB.NET AND 3D |
|||||||||||||||||||||||