windows service

Hi!

I'm creating a windows service where i want to execute an exe whenever the service is started.

I tried process.start("myexe") in the On_Start() event of the windows

service... but the service starts and stops as soon as it is started

saying that the service has no work to do.

please help!!!

[375 byte] By [MariamCR] at [2007-12-23]
# 1

If all you are trying to do with your service is launch a different exe you may want to look into using srvany.exe which was built by Microsoft ages ago to do just this.

BrendanGrant at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Hi Brendan Grant!

Thanks for your response, I'd appreciate it very much cause this is my first time building a service in .Net...

What I'm trying to do it's to execute a code (private function iniciar () ) which is in the service, then I also create a Setup Project in order to install and keep run the service on a server.

Actually my code look like this:

' -

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

Timer1.Enabled = True

iniciar()

aTimer = New System.Timers.Timer

AddHandler aTimer.Elapsed, AddressOf OnTimer

' por default --> aTimer.AutoReset(True)

' intervalo dado en segundos

aTimer.Interval = 900000 ' => pb! cada 15 min (5000 = 5 seg)

' dispara accion

aTimer.Enabled = True

End Sub

' -

Protected Overrides Sub OnStop()

aTimer.Enabled = False

End Sub

' -

Private Sub iniciar()....End Sub

' -

Private Sub aTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles aTimer.Elapsed

iniciar()

End Sub

MariamCR at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

While you've got an AddHandler statement to link the aTimer.Elapsed event to something... you don't appear to have an OnTimer() method within your code... you might want to change that call to aTimerElapsed.

Also... running a System.Timers.Timer is a Windows service isn't always the best idea... instead try using System.Threading.Timer instead which tends to be a little more reliable in this context.

BrendanGrant at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Hi!

I've resolve the problem about the system starting... now my service is running... but there's a problem with the --System.Timers.Timer --that I am using, It works perfect the first time but the Thread doesn't works again.

Could be possible the OnTimer method is correctly running only the fist time?

I also tryed using a --System.Threading.Timer -- but it's not working even by the first time.

-

These is a piece of how my code looks like:

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

Me.registroTxt.WriteLine("Sevice OnStart. " & Now())

Me.registroTxt.WriteLine()

Me.registroTxt.Flush()

''

AddHandler Me.Timer1.Elapsed, AddressOf OnTimer

' intervalo dado en segundos

Me.Timer1.Interval = 600000 ' => pb! cada 10 min (cifra en miliseg)

' worker thread

Me.Timer1.AutoReset = True

' dispara accion

Me.Timer1.Enabled = True

'-'

End Sub

Public Sub OnTimer(ByVal source As Object, ByVal e As ElapsedEventArgs)

Me.registroTxt.WriteLine("Sevice OnTimer. " & Now())

Me.registroTxt.WriteLine()

Me.registroTxt.Flush()

Me.iniciar()

End Sub

Protected Overrides Sub OnStop()

' Add code here to perform any tear-down necessary to stop your service.

Me.Timer1.Enabled = False

End Sub

#Region "Mariam..."

Private Sub iniciar()...End Sub

#End Region

Any examples, suggestions? Tips... ? I'd appreciate it very much.

As always, thanks a lot!!!

MariamCR at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Hi MariamCR,

The code is ok but, the service must use a proper user account.

If you want it to run the program when you start it, you must

  • Go to The Services tool of Windows (services.msc)

  • Fnd the service

  • Rght-click the service

  • Slect properties

  • On logon tab check Allow service to interact with desktop.

This works for me

Best regards,

Umut

UmutSIMSEK at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...