473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Windows Service with Timer

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
Timer1.Start()

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim MyLog As New EventLog ' create a new event log
' Check if the the Event Log Exists
If Not MyLog.SourceExists("Service") Then
MyLog.CreateEventSource("Service", "Service Log") ' Create Log
End If
MyLog.Source = "Service"

' Write to the Log
MyLog.WriteEntry("Service Log", "This is log on " & _
CStr(TimeOfDay), EventLogEntryType.Information)

End Sub

Nov 22 '05 #1
4 4180
On Wed, 25 Aug 2004 06:05:02 -0700, Henrik H wrote:
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


Which timer are you using? There are several timers available. If you're
using the one from the System.Windows.Forms.Timer, then it probably won't
work in a service unless you allow the service to interact with the
desktop. Try one of the other timers instead.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 22 '05 #2
Thanks Chris,
That worked :o)

My timer was a system.windows.form.timer??? hmm
Where can I set the "allow the service to interact with the
desktop."

But anyhow,, it worked when i've used the System.timers.timer,, se link

http://msdn.microsoft.com/library/de...classtopic.asp

"Chris Dunaway" <"dunawayc[[at]_lunchmeat" wrote:
On Wed, 25 Aug 2004 06:05:02 -0700, Henrik H wrote:
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


Which timer are you using? There are several timers available. If you're
using the one from the System.Windows.Forms.Timer, then it probably won't
work in a service unless you allow the service to interact with the
desktop. Try one of the other timers instead.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Nov 22 '05 #3
On Wed, 25 Aug 2004 08:27:11 -0700, Henrik H wrote:
Where can I set the "allow the service to interact with the
desktop."


I'm not sure how to set it programatically, but you can right click on the
service in the Services applet in the control panel and set it there.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 22 '05 #4
There is a bug in VS.2005. The toolbox for windows services creates a
Windows.Forms.Timer instead of a System.Threading.Timer.

So create your timers programmatically.

Here is a code snippet that works:

Imports System.Threading
Dim myTimer As System.Threading.Timer

In OnStart:
'start the system.threading.timer - its callback runs in a background thread
Dim myTmrCbck As New TimerCallback(AddressOf Timer_Elapsed)
myTimer = New System.Threading.Timer(myTmrCbck, Nothing, 1000, 1000)

in Time_Elapsed:
Private Sub Timer_Elapsed(ByVal state As Object)
Call Beep()
EventLog1.WriteEntry("Timer1 elapsed.")
End Sub

In OnStop:
myTimer.Dispose() 'stop timer

hth herbert
Mar 3 '06 #5

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

Similar topics

2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
2
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...
5
by: Dhilip Kumar | last post by:
Hi all, I have developed a windows service using the windows service project template in VS.NET. I have used three controls in the service, a timer, performance counter and a message queue...
3
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...
3
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...
4
by: Groundskeeper | last post by:
I can't seem to get a custom UnhandledException handler working for a Windows Service I'm writing in VB.NET. I've read the MSDN and tried various different placements of the AddHandler call, to no...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
5
by: Tom | last post by:
Using multiple System.Timers.Timer objects in a Windows Service for performing multi-thread activities in a periodic fashion. Timers are AutoReset=false, to only have a single timer execution...
4
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.