473,804 Members | 3,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows Service, timer doesn't tick

Others had the same problem and I understand that I have to use the
System.Timers.T imer 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.Sock ets
Imports System.Net.Dns
Imports System.Text
Imports System.IO
Public Class Service1

Private bTrigger As Boolean
Private myTimer As New System.Timers.T imer

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(By Val sender As System.Object, ByVal e As
System.EventArg s)

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
Sep 25 '06 #1
7 4806
I would abondon the timer method and use
System.Threadin g.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.Threadin g.Thread(Addres sOf
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:
Others had the same problem and I understand that I have to use the
System.Timers.T imer 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.Sock ets
Imports System.Net.Dns
Imports System.Text
Imports System.IO
Public Class Service1

Private bTrigger As Boolean
Private myTimer As New System.Timers.T imer

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(By Val sender As System.Object, ByVal e As
System.EventArg s)

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
Sep 25 '06 #2
PGC
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(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles myTimer.Tick
PGC
"Morten Snedker" <morten_spammen ot_ATdbconsult. dkwrote in message
news:g3******** *************** *********@4ax.c om...
Others had the same problem and I understand that I have to use the
System.Timers.T imer 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.Sock ets
Imports System.Net.Dns
Imports System.Text
Imports System.IO
Public Class Service1

Private bTrigger As Boolean
Private myTimer As New System.Timers.T imer

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(By Val sender As System.Object, ByVal e As
System.EventArg s)

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

Sep 25 '06 #3
Morten Snedker wrote:
Others had the same problem and I understand that I have to use the
System.Timers.T imer 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.Sock ets
Imports System.Net.Dns
Imports System.Text
Imports System.IO
Public Class Service1

Private bTrigger As Boolean
Private myTimer As New System.Timers.T imer

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

With myTimer
.Interval = 10000
.Enabled = True
.Start()
End With

LogFile(Now & ": " & vbTab & "Service started")

End Sub
What does the LogFile method do? Do you see the message that says
"Service Started" ? Does the service have the necessary permissions to
write to the log file?
<snip>

Sep 25 '06 #4
On 25 Sep 2006 06:48:40 -0700, "Izzy" <is************ @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(AddressO f 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(60 000)
If Minute(Now) = 44 Then ExecuteWOL()
Loop

End Sub
....
Thx again.
Regards /Snedker
Sep 26 '06 #5
There needs to be a thWorker.Abort( ) in your OnStop() method.

Izzy
Morten Snedker wrote:
On 25 Sep 2006 06:48:40 -0700, "Izzy" <is************ @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(AddressO f 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(60 000)
If Minute(Now) = 44 Then ExecuteWOL()
Loop

End Sub
...
Thx again.
Regards /Snedker
Sep 26 '06 #6
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.

Sep 26 '06 #7

Chris Dunaway wrote:
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.
Can you give a code example of how to properly shut down a thread?

Izzy

Sep 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2967
by: Lloyd Sheen | last post by:
I have a problem where I cannot read (all the time) a file when caught by the FileSystemWatcher. What I have done to get past this is create an array of events (when I receive one I add it to the list). I then enable a timer which will attempt to process the first one in the list. The timer code is never executed. There is a handles clause on the timer and using the debugger I have watched the code for the FileSystemWatcher execute. ...
3
857
by: Henrik H | last post by:
Hi all! I have a Windows service, where I want to use a timer.. But it does not Seems work?? It does not catch the Timer1.tick event ??? But the code works on a form??? Can anyone help me, please?? "Onstart" - I set: Timer1.Interval = 5000 Timer1.Enabled = True
2
4716
by: Jesper Stocholm | last post by:
I have created a simple service which just copies a fil to a new file with a new name on certain intervals (the service implements a timer). I have no problems installing the service and the log-entries I have created on stop() and start() in the eventlog works fine as well. However - the actual task is never executed. I catch the execution of the tick and in the finally-block I write to the eventlog as well. But only one entry is creates...
7
33589
by: J. Hill | last post by:
I have a Windows Service with a timer but the .Tick event is not being fired/called. Don't know what code to include...I enabled and started the timer...I have the exact same code in a Windows form and it works fine, but in the service: nothing. .... this.components = new System.ComponentModel.Container(); this.tmrTimer = new System.Windows.Forms.Timer(this.components); // // tmrTimer //
3
2201
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My issue is simply getting timers to work. I have a DatabaseManager.DataManagerFacade which contains a timer. Every 6 seconds it updates stock data using an online webservice. I wrote have 2 possible startup projects to make the service work. One...
3
350
by: Yves Royer | last post by:
Hi all, I have a little question about Windows Services. For my app i need 3 different Windows Services, each with its own functionality. I'm trying to make a test service to see what happens when a service is stopped or started. What i'm trying do do now is adding a timer so after a specific period of time the service does something, in the example below writing to a text
3
9545
by: Scott H | last post by:
Hello, I'm having a go at writing my first Windows Service in VB.NET and I'm having a problem. I have it installed ok and started the service sucessfully, I can stop it, and restart it fine, the problem is it doesn't appear to be running any of my code whatsoever. I dropped a Timer Control on the designer, set it to disabled with an interval of 5000, went to the code window and on the OnStart Event, typed in: Timer1.Enabled=True
1
3961
by: Manuel | last post by:
Used VS2005 to create a windows service and I can't make a timer trigger the tick/elapsed event. On the form viewer I dragged a timer from the components section of the toolbar, enabled it but the tick event does not fire! I tried this other method but the elapsed event does not fire. ---------- Private WithEvents MyTmr As New System.Timers.Timer(1000) Private Sub MyTmr_Elapsed(ByVal sender As Object, ByVal e As
2
5812
by: Rico | last post by:
Hello, I'm trying run a process at regular intervals using a windows service. the problem is, the timer_tick event doesn't appear to be working. I've written the following code as a test; Public Class FileName Protected Overrides Sub OnStart(ByVal args() As String)
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.