473,626 Members | 3,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2345
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.ServiceP rocess.ServiceB ase

Private WithEvents Timer1 As System.Timers.T imer

Public Sub New()
MyBase.New()

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

' Add any initialization after the InitializeCompo nent() call

AddHandler AppDomain.Curre ntDomain.Unhand ledException, _
AddressOf AppDomain_Unhan dledException
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.E lapsedEventArgs ) Handles Timer1.Elapsed
Try
EventLog.WriteE ntry("Timer elapsed handler started",
EventLogEntryTy pe.Information)
' Do work here
EventLog.WriteE ntry("Timer elapsed handler finished",
EventLogEntryTy pe.Information)
Catch ex As Exception
EventLog.WriteE ntry(ex.ToStrin g(), EventLogEntryTy pe.Error)
End Try
End Sub

Private Sub AppDomain_Unhan dledException(B yVal sender As Object, _
ByVal e As UnhandledExcept ionEventArgs)
EventLog.WriteE ntry(e.Exceptio nObject.ToStrin g(),
EventLogEntryTy pe.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.Eve ntLog 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" ?>
<configuratio n>
<system.diagnos tics>
<trace autoflush="true " indentsize="4">
<listeners>
<add name="myListene r" type="System.Di agnostics.TextW riterTraceListe ner"
initializeData= "MyService. log" />
</listeners>
</trace>
</system.diagnost ics>
</configuration>
Note the UnhandledExcept ion 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*******@disc ussions.microso ft.com> wrote in message
news:2A******** *************** ***********@mic rosoft.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******** ******@TK2MSFTN GP10.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.ServiceP rocess.ServiceB ase

Private WithEvents Timer1 As System.Timers.T imer

Public Sub New()
MyBase.New()

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

' Add any initialization after the InitializeCompo nent() call

AddHandler AppDomain.Curre ntDomain.Unhand ledException, _
AddressOf AppDomain_Unhan dledException
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.E lapsedEventArgs ) Handles Timer1.Elapsed
Try
EventLog.WriteE ntry("Timer elapsed handler started",
EventLogEntryTy pe.Information)
' Do work here
EventLog.WriteE ntry("Timer elapsed handler finished",
EventLogEntryTy pe.Information)
Catch ex As Exception
EventLog.WriteE ntry(ex.ToStrin g(), EventLogEntryTy pe.Error)
End Try
End Sub

Private Sub AppDomain_Unhan dledException(B yVal sender As Object, _
ByVal e As UnhandledExcept ionEventArgs)
EventLog.WriteE ntry(e.Exceptio nObject.ToStrin g(),
EventLogEntryTy pe.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.Eve ntLog 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" ?>
<configuratio n>
<system.diagnos tics>
<trace autoflush="true " indentsize="4">
<listeners>
<add name="myListene r" type="System.Di agnostics.TextW riterTraceListe ner" initializeData= "MyService. log" />
</listeners>
</trace>
</system.diagnost ics>
</configuration>
Note the UnhandledExcept ion 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*******@disc ussions.microso ft.com> wrote in message
news:2A******** *************** ***********@mic rosoft.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("t est"
'Création des logs s'il n'existe pas
If Not Log.SourceExist s("FusionDec" ) The
Log.CreateEvent Source("FusionD ec", "FusionDec"
End I
Log.Source = "FusionDec

'option du FileSystemWatch er
fwFichierFusion .Path = "c:\Test2\
fwFichierFusion .Filter = "
fwFichierFusion .IncludeSubdire ctories = Tru

fwFichierFusion .EnableRaisingE vents = 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.GetExcepti on.ToString()
EcrireErreur(Er r.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.SourceExist s("FusionDec" ) Then
Log.CreateEvent Source("FusionD ec", "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.Eve ntLog 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.Diagnost ics.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.WriteE ntry("FusionDec démarrer avec succès")
EventLog.WriteE ntry("Erreur lors du démarrage du service : " & vbCrLf & ex.ToString)

Hope this helps
Jay

"Eric" <an*******@disc ussions.microso ft.com> wrote in message
news:6D******** *************** ***********@mic rosoft.com... Hi,

This is what I have in my OnStart :

Try
EcrireErreur("t est")
'Création des logs s'il n'existe pas.
If Not Log.SourceExist s("FusionDec" ) Then
Log.CreateEvent Source("FusionD ec", "FusionDec" )
End If
Log.Source = "FusionDec"

'option du FileSystemWatch er.
fwFichierFusion .Path = "c:\Test2\"
fwFichierFusion .Filter = ""
fwFichierFusion .IncludeSubdire ctories = True

fwFichierFusion .EnableRaisingE vents = 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.GetExcepti on.ToString())
EcrireErreur(Er r.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
2884
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
4182
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 send mail code is executed, other necessary Windows Services have been terminated before it can actually send the mail. I've updated my HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services registry DependOnService value including SMPTSVC and...
3
6133
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 for the application doesn't appear - however, I can see the process in the TaskManager. Now, feel free to mention that starting a regular windows application
5
2291
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 Application, does not work while I run it with Windows services. In my Windows application I am able to zip the whole 900Mb without any problems, but in my windows services I am not able to zip the whole 900Mb. In Windows Services it throws an...
2
4480
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 seperate process. If my windows application starts running,only if it completes fully, then my windows services should continue its execution. My main process is Windows service.
1
4842
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 seperate process. If my windows application starts running,only if it completes fully, then my windows services should continue its execution. My main process is Windows service.
5
3295
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? Why do I need to set a property within my code to the service name? Are all these required or am I just doing this for consistency purposes?
0
6113
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). Much of the contents of this article are tested on Windows XP professional SP2 by the author. Some instances of this article may be altogether different/missing on Windows Vista, XP and other Windows NT systems, but I have tried to write a...
0
30205
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 a comprehensive list of all autostart locations for Windows OSes: NOTE : These are some abbreviations used in this list. Please note them carefully: HKCU = HKEY_CURRENT_USER HKLM = HKEY_LOCAL_MACHINE
0
8202
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
8707
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...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
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
8510
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...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5575
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
2628
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
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.