473,379 Members | 1,367 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,379 software developers and data experts.

Windows Services

Hi

I built a service that watch file in a directory. I use a simple filewatcher and add a handler to do something when a file is created. I have the following problem. I can build it, I can install it, I can start it but when I start it I have a message that the service is stop because it does nothing and it doesn't execute the code in the service. Please if you have any idea it will be appreciate

Thanks yo

Nov 20 '05 #1
5 2313
Eric,
Are you doing too much work in the OnStart method?

Have you tried debugging your service:
http://msdn.microsoft.com/library/de...owsservice.asp

Normally what I do is include a healthy amount of event log messages (via
..EventLog) especially in any asynchronous events that may be raised within
the service. Plus I will include Debug.Write & Trace.Write in strategic
locations.

Something like:

Public Class MyService
Inherits System.ServiceProcess.ServiceBase

Private WithEvents Timer1 As System.Timers.Timer

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call

AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf AppDomain_UnhandledException
End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Timer1.Start()
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
Timer1.Stop()
End Sub

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Try
EventLog.WriteEntry("Timer elapsed handler started",
EventLogEntryType.Information)
' Do work here
EventLog.WriteEntry("Timer elapsed handler finished",
EventLogEntryType.Information)
Catch ex As Exception
EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error)
End Try
End Sub

Private Sub AppDomain_UnhandledException(ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs)
EventLog.WriteEntry(e.ExceptionObject.ToString(),
EventLogEntryType.Error)
End Sub
End Class

Note EventLog is inherited from ServiceBase and will be the normal event log
for your service, you can override ServiceBase.EventLog if you want to use a
different Event log.

I configure Debug & Trace to go to a log file.

In your app.config of your Windows Service project:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener"
initializeData="MyService.log" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Note the UnhandledException event is not working as I expected in the above,
so I put the Elapsed event in its own Try/Catch.

Hope this helps
Jay

"Eric" <an*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
Hi,

I built a service that watch file in a directory. I use a simple filewatcher and add a handler to do something when a file is created. I have
the following problem. I can build it, I can install it, I can start it but
when I start it I have a message that the service is stop because it does
nothing and it doesn't execute the code in the service. Please if you have
any idea it will be appreciate.
Thanks you

Nov 20 '05 #2
Another choice is to use the Debug Viewer utility (free) from
www.sysinternals.com. With this program you can catch Debug.Writeline from
any program (including services) without any involvement from the IDE.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
Eric,
Are you doing too much work in the OnStart method?

Have you tried debugging your service:
http://msdn.microsoft.com/library/de...owsservice.asp
Normally what I do is include a healthy amount of event log messages (via
.EventLog) especially in any asynchronous events that may be raised within
the service. Plus I will include Debug.Write & Trace.Write in strategic
locations.

Something like:

Public Class MyService
Inherits System.ServiceProcess.ServiceBase

Private WithEvents Timer1 As System.Timers.Timer

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call

AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf AppDomain_UnhandledException
End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things ' in motion so your service can do its work.
Timer1.Start()
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
Timer1.Stop()
End Sub

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Try
EventLog.WriteEntry("Timer elapsed handler started",
EventLogEntryType.Information)
' Do work here
EventLog.WriteEntry("Timer elapsed handler finished",
EventLogEntryType.Information)
Catch ex As Exception
EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error)
End Try
End Sub

Private Sub AppDomain_UnhandledException(ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs)
EventLog.WriteEntry(e.ExceptionObject.ToString(),
EventLogEntryType.Error)
End Sub
End Class

Note EventLog is inherited from ServiceBase and will be the normal event log for your service, you can override ServiceBase.EventLog if you want to use a different Event log.

I configure Debug & Trace to go to a log file.

In your app.config of your Windows Service project:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="MyService.log" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Note the UnhandledException event is not working as I expected in the above, so I put the Elapsed event in its own Try/Catch.

Hope this helps
Jay

"Eric" <an*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
Hi,

I built a service that watch file in a directory. I use a simple filewatcher and add a handler to do something when a file is created. I

have the following problem. I can build it, I can install it, I can start it but when I start it I have a message that the service is stop because it does
nothing and it doesn't execute the code in the service. Please if you have
any idea it will be appreciate.

Thanks you


Nov 20 '05 #3
Hi

This is what I have in my OnStart

Tr
EcrireErreur("test"
'Création des logs s'il n'existe pas
If Not Log.SourceExists("FusionDec") The
Log.CreateEventSource("FusionDec", "FusionDec"
End I
Log.Source = "FusionDec

'option du FileSystemWatcher
fwFichierFusion.Path = "c:\Test2\
fwFichierFusion.Filter = "
fwFichierFusion.IncludeSubdirectories = Tru

fwFichierFusion.EnableRaisingEvents = Tru

'Ajout de l'action évènement
AddHandler fwFichierFusion.Created, AddressOf FusionDe

Log.WriteEntry("FusionDec démarrer avec succès"
Catch ex As Exceptio
Debug.WriteLine(Err.GetException.ToString()
EcrireErreur(Err.GetException.ToString()
Log.WriteEntry("Erreur lors du démarrage du service : " & vbCrLf & ex.ToString
End Tr

I think it isn't to much in the OnStart() and I can't debug it because it start and few second after it stop.

by the way thanks for trying
Nov 20 '05 #4
Eric,
Which of the following event log messages are you seeing?
Log.WriteEntry("FusionDec démarrer avec succès")
Log.WriteEntry("Erreur lors du démarrage du service : " & vbCrLf & ex.ToString)

Depending on which you see will indicate if your service is starting or not.

If you are not seeing either then the OnStart is not being reached or is
terminating early.

Are you calling MyBase.OnStart from your OnStart?

More importantly! As I stated, put a try catch in your FusionDec method, so
you know if it failed or not, plus put logging to know it was called.

This way you if you see the success message you know the service started,
then you can look for the FusionDec messages, so you know
You do know that you can have the Windows Installer create the Event Source
& Event Log for you?
'Création des logs s'il n'existe pas.
If Not Log.SourceExists("FusionDec") Then
Log.CreateEventSource("FusionDec", "FusionDec")
End If
Log.Source = "FusionDec"
Drag an EventLog object from the toolbox onto the design surface of your
Windows Service. Set the Log & Source properties. Click the "Add Installer"
link (or right click on EventLog on the designer). This will add an
Installer to your Service Installer.

Also, rather then using your Log property I override the
ServiceBase.EventLog property and return the object created by dragging an
EventLog to the designer. This causes all the automatic messages as well as
my messages to go to the same log. (read you can leave AutoLog=True and the
auto log messages will go to the event log of your choosing).

Public Overrides ReadOnly Property EventLog() As
System.Diagnostics.EventLog
Get
Return Me.EventLog1
' Seeing as you have a Log property you can return it.
Return Log
End Get
End Property

Then I would simply use EventLog
EventLog.WriteEntry("FusionDec démarrer avec succès")
EventLog.WriteEntry("Erreur lors du démarrage du service : " & vbCrLf & ex.ToString)

Hope this helps
Jay

"Eric" <an*******@discussions.microsoft.com> wrote in message
news:6D**********************************@microsof t.com... Hi,

This is what I have in my OnStart :

Try
EcrireErreur("test")
'Création des logs s'il n'existe pas.
If Not Log.SourceExists("FusionDec") Then
Log.CreateEventSource("FusionDec", "FusionDec")
End If
Log.Source = "FusionDec"

'option du FileSystemWatcher.
fwFichierFusion.Path = "c:\Test2\"
fwFichierFusion.Filter = ""
fwFichierFusion.IncludeSubdirectories = True

fwFichierFusion.EnableRaisingEvents = True

'Ajout de l'action évènement.
AddHandler fwFichierFusion.Created, AddressOf FusionDec

Log.WriteEntry("FusionDec démarrer avec succès")
Catch ex As Exception
Debug.WriteLine(Err.GetException.ToString())
EcrireErreur(Err.GetException.ToString())
Log.WriteEntry("Erreur lors du démarrage du service : " & vbCrLf & ex.ToString) End Try
I think it isn't to much in the OnStart() and I can't debug it because it start and few second after it stop.
by the way thanks for trying

Nov 20 '05 #5
Thanks for your help but I found that it was a security problem. The user I use to install my service haven't the right to create a new EventLog.

great thank for you help. I found out with this little tip : You do know that you can have the Windows Installer create the Event Source & Event Log for you?

bye

Nov 20 '05 #6

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

Similar topics

0
by: Stefan Hinz | last post by:
Degan, jumping in to try and solve some problems that look pretty obvious to me ... > #options for default service (mysqld2) > (mysqld2) It should be , not (mysqld2).
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
3
by: illegal.prime | last post by:
Hi all, I have a service that needs to start a regular windows application. I'm running the service as ServiceAccount.LocalSystem. But, when it starts the process (using Process.Start) the GUI...
5
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
This code works fine in Windows Application. In Windows Application, I am able to zip the image files properly and it totally contains 900MB My problem is the same code which I used in my Windows...
2
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as...
1
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as...
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
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Overview NOTE- This complete article on "Windows Autorun FAQs" applies theoretically to all Windows NT-based OSes till Windows Vista (and probably Vista's successors too)....
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all the autostart locations for windows? Ans: Here is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.