473,586 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

windows (vb.net 1.1) service - threading issue

Hi

I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. Has anyone seen this
before? What can I do to prevent it?

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

Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
' stop the timer while the process runs
If Switch.TraceVer bose Then
Trace.WriteLine ("Timer elapsed")
End If
Timer1.Stop()
DoSomething()
' restart the timer
Timer1.Start()
End Sub
Public Sub DoSomething()
If Switch.TraceVer bose Then
Trace.WriteLine ("Executing DoSomething")
End If


Sep 5 '08 #1
9 2174
br************* *******@yahoo.c o.uk wrote:
Hi

I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. Has anyone seen this
before? What can I do to prevent it?

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

Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
' stop the timer while the process runs
If Switch.TraceVer bose Then
Trace.WriteLine ("Timer elapsed")
End If
Timer1.Stop()
DoSomething()
' restart the timer
Timer1.Start()
End Sub
Public Sub DoSomething()
If Switch.TraceVer bose Then
Trace.WriteLine ("Executing DoSomething")
End If


Here's some code I use to do some work at 01:15 each night. It may give
you some ideas.

Private mbBusy As Boolean
Private Timer1 As New System.Timers.T imer(60000)

Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
System.Timers.E lapsedEventArgs )

If mbBusy Then Exit Sub

Dim z As DateTime
Dim s As String = Format(z.Now, "HH:mm")

Select Case s
Case "01:15"

mbBusy = True
LogItem("BEGIN" )

DoJob()

LogItem("END")
mbBusy = False

Case Else
'LogItem("Revie wService " & s)

End Select

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.

LogItem("Review Service Started")

AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
Timer1.Start()
Timer1.Enabled = True

End Sub

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

Timer1.Enabled = False
Timer1.Stop()
RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed

LogItem("Review Service Stopped")

End Sub
LogItem just appends some text to a (log) file. DoJob does the work I
want done. It seems to work okay.

HTH
Sep 5 '08 #2
On Sep 5, 5:29*pm, Jason Keats <jke...@melbpcD eleteThis.org.a uwrote:
brendan_gallagh er_2...@yahoo.c o.uk wrote:
Hi
I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. *In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. *Has anyone seen this
before? *What can I do to prevent it?
* * Protected Overrides Sub OnStop()
* * * * ' Add code here to perform any tear-down necessary to stop
your service.
* * * * Timer1.Enabled = False
* * End Sub
* * Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVale
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
* * * * ' stop the timer while the process runs
* * * * If Switch.TraceVer bose Then
* * * * * * Trace.WriteLine ("Timer elapsed")
* * * * End If
* * * * Timer1.Stop()
* * * * DoSomething()
* * * * ' restart the timer
* * * * Timer1.Start()
* * End Sub
* * Public Sub DoSomething()
* * * * If Switch.TraceVer bose Then
* * * * * * Trace.WriteLine ("Executing DoSomething")
* * * * End If

Here's some code I use to do some work at 01:15 each night. It may give
you some ideas.

Private mbBusy As Boolean
Private Timer1 As New System.Timers.T imer(60000)

Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
System.Timers.E lapsedEventArgs )

* * * * If mbBusy Then Exit Sub

* * * * Dim z As DateTime
* * * * Dim s As String = Format(z.Now, "HH:mm")

* * * * Select Case s
* * * * * * * * Case "01:15"

* * * * * * * * * * * * mbBusy = True
* * * * * * * * * * * * LogItem("BEGIN" )

* * * * * * * * * * * * DoJob()

* * * * * * * * * * * * LogItem("END")
* * * * * * * * * * * * mbBusy = False

* * * * * * * * Case Else
* * * * * * * * * * * * 'LogItem("Revie wService "& s)

* * * * End Select

End Sub

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

* * * * LogItem("Review Service Started")

* * * * AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
* * * * Timer1.Start()
* * * * Timer1.Enabled = True

End Sub

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

* * * * Timer1.Enabled = False
* * * * Timer1.Stop()
* * * * RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed

* * * * LogItem("Review Service Stopped")

End Sub

LogItem just appends some text to a (log) file. DoJob does the work I
want done. It seems to work okay.

HTH- Hide quoted text -

- Show quoted text -
Thanks Jason. Do you think there is anything I can do on the
threading side to prevent two threads executing the the DoSomething()
method at the same time?
Sep 5 '08 #3
On 2008-09-05, br************* *******@yahoo.c o.uk <br************ ********@yahoo. co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcD eleteThis.org.a uwrote:
>brendan_gallag her_2...@yahoo. co.uk wrote:
Hi
I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. ?In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. ?Has anyone seen this
before? ?What can I do to prevent it?
? ? Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop
your service.
? ? ? ? Timer1.Enabled = False
? ? End Sub
? ? Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Executing DoSomething")
? ? ? ? End If
?

Here's some code I use to do some work at 01:15 each night. It may give
you some ideas.

Private mbBusy As Boolean
Private Timer1 As New System.Timers.T imer(60000)

Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
System.Timers. ElapsedEventArg s)

? ? ? ? If mbBusy Then Exit Sub

? ? ? ? Dim z As DateTime
? ? ? ? Dim s As String = Format(z.Now, "HH:mm")

? ? ? ? Select Case s
? ? ? ? ? ? ? ? Case "01:15"

? ? ? ? ? ? ? ? ? ? ? ? mbBusy = True
? ? ? ? ? ? ? ? ? ? ? ? LogItem("BEGIN" )

? ? ? ? ? ? ? ? ? ? ? ? DoJob()

? ? ? ? ? ? ? ? ? ? ? ? LogItem("END")
? ? ? ? ? ? ? ? ? ? ? ? mbBusy = False

? ? ? ? ? ? ? ? Case Else
? ? ? ? ? ? ? ? ? ? ? ? 'LogItem("Revie wService " & s)

? ? ? ? End Select

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.

? ? ? ? LogItem("Review Service Started")

? ? ? ? AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
? ? ? ? Timer1.Start()
? ? ? ? Timer1.Enabled = True

End Sub

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

? ? ? ? Timer1.Enabled = False
? ? ? ? Timer1.Stop()
? ? ? ? RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed

? ? ? ? LogItem("Review Service Stopped")

End Sub

LogItem just appends some text to a (log) file. DoJob does the work I
want done. It seems to work okay.

HTH- Hide quoted text -

- Show quoted text -

Thanks Jason. Do you think there is anything I can do on the
threading side to prevent two threads executing the the DoSomething()
method at the same time?
Yes, put in a lock...

In your service class

Private Shared _lock As New Object()
In your DoSomething() Method:

Private Sub DoSomething ()
SyncLock _lock ' only one thread at a time can enter here
' Do your cool stuff
End SyncLock
End Sub

--
Tom Shelton
Sep 5 '08 #4
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:
On 2008-09-05, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2...@yahoo. co.ukwrote:


On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcD eleteThis.org.a uwrote:
brendan_gallagh er_2...@yahoo.c o.uk wrote:
Hi
I am seeing some strange behaviour on a windows (vb.net 1.1) service..
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. ?In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. ?Has anyone seen this
before? ?What can I do to prevent it?
? ? Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop
your service.
? ? ? ? Timer1.Enabled = False
? ? End Sub
? ? Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Executing DoSomething")
? ? ? ? End If
?
Here's some code I use to do some work at 01:15 each night. It may give
you some ideas.
Private mbBusy As Boolean
Private Timer1 As New System.Timers.T imer(60000)
Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
System.Timers.E lapsedEventArgs )
? ? ? ? If mbBusy Then Exit Sub
? ? ? ? Dim z As DateTime
? ? ? ? Dim s As String = Format(z.Now, "HH:mm")
? ? ? ? Select Case s
? ? ? ? ? ? ? ? Case "01:15"
? ? ? ? ? ? ? ? ? ? ? ? mbBusy = True
? ? ? ? ? ? ? ? ? ? ? ? LogItem("BEGIN" )
? ? ? ? ? ? ? ? ? ? ? ? DoJob()
? ? ? ? ? ? ? ? ? ? ? ? LogItem("END")
? ? ? ? ? ? ? ? ? ? ? ? mbBusy = False
? ? ? ? ? ? ? ? Case Else
? ? ? ? ? ? ? ? ? ? ? ? 'LogItem("Revie wService " & s)
? ? ? ? End Select
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.
? ? ? ? LogItem("Review Service Started")
? ? ? ? AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
? ? ? ? Timer1.Start()
? ? ? ? Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop your service.
? ? ? ? Timer1.Enabled = False
? ? ? ? Timer1.Stop()
? ? ? ? RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
? ? ? ? LogItem("Review Service Stopped")
End Sub
LogItem just appends some text to a (log) file. DoJob does the work I
want done. It seems to work okay.
HTH- Hide quoted text -
- Show quoted text -
Thanks Jason. *Do you think there is anything I can do on the
threading side to prevent two threads executing the the DoSomething()
method at the same time?

Yes, put in a lock...

In your service class

Private Shared _lock As New Object()

In your DoSomething() Method:

Private Sub DoSomething ()
* * * * SyncLock _lock ' only one thread at a time can enter here
* * * * * * * * ' Do your cool stuff
* * * * End SyncLock
End Sub

--
Tom Shelton- Hide quoted text -

- Show quoted text -
Thanks Tom. Is there any chance of a deadlock happening when using
SyncLock in this way?
Sep 6 '08 #5
On 2008-09-06, br************* *******@yahoo.c o.uk <br************ ********@yahoo. co.ukwrote:
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:
>On 2008-09-05, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2...@yahoo. co.ukwrote:


On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcD eleteThis.org.a uwrote:
brendan_gallag her_2...@yahoo. co.uk wrote:
Hi
I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. ?In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. ?Has anyone seen this
before? ?What can I do to prevent it?
? ? Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop
your service.
? ? ? ? Timer1.Enabled = False
? ? End Sub
? ? Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Executing DoSomething")
? ? ? ? End If
?
>Here's some code I use to do some work at 01:15 each night. It may give
you some ideas.
>Private mbBusy As Boolean
Private Timer1 As New System.Timers.T imer(60000)
>Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
System.Timers. ElapsedEventArg s)
>? ? ? ? If mbBusy Then Exit Sub
>? ? ? ? Dim z As DateTime
? ? ? ? Dim s As String = Format(z.Now, "HH:mm")
>? ? ? ? Select Case s
? ? ? ? ? ? ? ? Case "01:15"
>? ? ? ? ? ? ? ? ? ? ? ? mbBusy = True
? ? ? ? ? ? ? ? ? ? ? ? LogItem("BEGIN" )
>? ? ? ? ? ? ? ? ? ? ? ? DoJob()
>? ? ? ? ? ? ? ? ? ? ? ? LogItem("END")
? ? ? ? ? ? ? ? ? ? ? ? mbBusy = False
>? ? ? ? ? ? ? ? Case Else
? ? ? ? ? ? ? ? ? ? ? ? 'LogItem("Revie wService " & s)
>? ? ? ? End Select
>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.
>? ? ? ? LogItem("Review Service Started")
>? ? ? ? AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
? ? ? ? Timer1.Start()
? ? ? ? Timer1.Enabled = True
>End Sub
>Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop your service.
>? ? ? ? Timer1.Enabled = False
? ? ? ? Timer1.Stop()
? ? ? ? RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
>? ? ? ? LogItem("Review Service Stopped")
>End Sub
>LogItem just appends some text to a (log) file. DoJob does the work I
want done. It seems to work okay.
>HTH- Hide quoted text -
>- Show quoted text -
Thanks Jason. *Do you think there is anything I can do on the
threading side to prevent two threads executing the the DoSomething()
method at the same time?

Yes, put in a lock...

In your service class

Private Shared _lock As New Object()

In your DoSomething() Method:

Private Sub DoSomething ()
* * * * SyncLock _lock ' only one thread at a time can enter here
* * * * * * * * ' Do your cool stuff
* * * * End SyncLock
End Sub

--
Tom Shelton- Hide quoted text -

- Show quoted text -

Thanks Tom. Is there any chance of a deadlock happening when using
SyncLock in this way?
It depends on what you do inside the lock... If for some reason, a thread
enters the lock, and never exits - then yes, you could deadlock.

--
Tom Shelton
Sep 7 '08 #6
On Sep 7, 5:43*am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:
On 2008-09-06, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2...@yahoo. co.ukwrote:


On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:
On 2008-09-05, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2....@yahoo .co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcD eleteThis.org.a uwrote:
brendan_gallagh er_2...@yahoo.c o.uk wrote:
Hi
I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long timeto
do this. ?In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. ?Has anyone seen this
before? ?What can I do to prevent it?
? ? Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop
your service.
? ? ? ? Timer1.Enabled = False
? ? End Sub
? ? Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Executing DoSomething")
? ? ? ? End If
?
Here's some code I use to do some work at 01:15 each night. It may give
you some ideas.
Private mbBusy As Boolean
Private Timer1 As New System.Timers.T imer(60000)
Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
System.Timers.E lapsedEventArgs )
? ? ? ? If mbBusy Then Exit Sub
? ? ? ? Dim z As DateTime
? ? ? ? Dim s As String = Format(z.Now, "HH:mm")
? ? ? ? Select Case s
? ? ? ? ? ? ? ? Case "01:15"
? ? ? ? ? ? ? ? ? ? ? ? mbBusy = True
? ? ? ? ? ? ? ? ? ? ? ? LogItem("BEGIN" )
? ? ? ? ? ? ? ? ? ? ? ? DoJob()
? ? ? ? ? ? ? ? ? ? ? ? LogItem("END")
? ? ? ? ? ? ? ? ? ? ? ? mbBusy = False
? ? ? ? ? ? ? ? Case Else
? ? ? ? ? ? ? ? ? ? ? ? 'LogItem("Revie wService " & s)
? ? ? ? End Select
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.
? ? ? ? LogItem("Review Service Started")
? ? ? ? AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
? ? ? ? Timer1.Start()
? ? ? ? Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop your service.
? ? ? ? Timer1.Enabled = False
? ? ? ? Timer1.Stop()
? ? ? ? RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
? ? ? ? LogItem("Review Service Stopped")
End Sub
LogItem just appends some text to a (log) file. DoJob does the workI
want done. It seems to work okay.
HTH- Hide quoted text -
- Show quoted text -
Thanks Jason. *Do you think there is anything I can do on the
threading side to prevent two threads executing the the DoSomething()
method at the same time?
Yes, put in a lock...
In your service class
Private Shared _lock As New Object()
In your DoSomething() Method:
Private Sub DoSomething ()
* * * * SyncLock _lock ' only one thread at a time can enter here
* * * * * * * * ' Do your cool stuff
* * * * End SyncLock
End Sub
--
Tom Shelton- Hide quoted text -
- Show quoted text -
Thanks Tom. *Is there any chance of a deadlock happening when using
SyncLock in this way?

It depends on what you do inside the lock... *If for some reason, a thread
enters the lock, and never exits - then yes, you could deadlock.

--
Tom Shelton- Hide quoted text -

- Show quoted text -
thanks again. Is there any way to avoid that happening i.e. kill the
thread after a certain lenght of time?
Sep 7 '08 #7
On Sep 7, 8:49*am, "brendan_gallag her_2...@yahoo. co.uk"
<brendan_gallag her_2...@yahoo. co.ukwrote:
On Sep 7, 5:43*am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:


On 2008-09-06, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2....@yahoo .co.ukwrote:
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:
>On 2008-09-05, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2...@yahoo. co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcD eleteThis.org.a uwrote:
>brendan_gallag her_2...@yahoo. co.uk wrote:
Hi
I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsede vent
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. ?In the meantime, the Timer1_Elapsed event fires againand a
new thread seems to try and also tries to execute Timer1.Stop()..
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. ?Has anyone seen this
before? ?What can I do to prevent it?
? ? Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop
your service.
? ? ? ? Timer1.Enabled = False
? ? End Sub
? ? Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Executing DoSomething")
? ? ? ? End If
?
>Here's some code I use to do some work at 01:15 each night. It may give
>you some ideas.
>Private mbBusy As Boolean
>Private Timer1 As New System.Timers.T imer(60000)
>Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
>System.Timers. ElapsedEventArg s)
>? ? ? ? If mbBusy Then Exit Sub
>? ? ? ? Dim z As DateTime
>? ? ? ? Dim s As String = Format(z.Now, "HH:mm")
>? ? ? ? Select Case s
>? ? ? ? ? ? ? ? Case "01:15"
>? ? ? ? ? ? ? ? ? ? ? ? mbBusy = True
>? ? ? ? ? ? ? ? ? ? ? ? LogItem("BEGIN" )
>? ? ? ? ? ? ? ? ? ? ? ? DoJob()
>? ? ? ? ? ? ? ? ? ? ? ? LogItem("END")
>? ? ? ? ? ? ? ? ? ? ? ? mbBusy = False
>? ? ? ? ? ? ? ? Case Else
>? ? ? ? ? ? ? ? ? ? ? ? 'LogItem("Revie wService " & s)
>? ? ? ? End Select
>End Sub
>Protected Overrides Sub OnStart(ByVal args() As String)
>? ? ? ? ' Add code here to start your service. This method shouldset things
>? ? ? ? ' in motion so your service can do its work.
>? ? ? ? LogItem("Review Service Started")
>? ? ? ? AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
>? ? ? ? Timer1.Start()
>? ? ? ? Timer1.Enabled = True
>End Sub
>Protected Overrides Sub OnStop()
>? ? ? ? ' Add code here to perform any tear-down necessary to stop your service.
>? ? ? ? Timer1.Enabled = False
>? ? ? ? Timer1.Stop()
>? ? ? ? RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
>? ? ? ? LogItem("Review Service Stopped")
>End Sub
>LogItem just appends some text to a (log) file. DoJob does the work I
>want done. It seems to work okay.
>HTH- Hide quoted text -
>- Show quoted text -
Thanks Jason. *Do you think there is anything I can do on the
threading side to prevent two threads executing the the DoSomething()
method at the same time?
>Yes, put in a lock...
>In your service class
>Private Shared _lock As New Object()
>In your DoSomething() Method:
>Private Sub DoSomething ()
>* * * * SyncLock _lock ' only one thread at a time can enterhere
>* * * * * * * * ' Do your cool stuff
>* * * * End SyncLock
>End Sub
>--
>Tom Shelton- Hide quoted text -
>- Show quoted text -
Thanks Tom. *Is there any chance of a deadlock happening when using
SyncLock in this way?
It depends on what you do inside the lock... *If for some reason, a thread
enters the lock, and never exits - then yes, you could deadlock.
--
Tom Shelton- Hide quoted text -
- Show quoted text -

thanks again. *Is there any way to avoid that happening i.e. kill the
thread after a certain lenght of time?- Hide quoted text -

- Show quoted text -
I have just discovered that the Monitor class resolves this:
Public Sub Foo()
Dim sText As String
Dim objLock As Object = New Object()
Try
Monitor.Enter(o bjLock)
Try
sText = "Hello"
Finally
Monitor.Exit(ob jLock)
End Try
Catch e As Exception
MessageBox.Show (e.Message)
End Try
End Sub
Sep 8 '08 #8
On 2008-09-08, br************* *******@yahoo.c o.uk <br************ ********@yahoo. co.ukwrote:
On Sep 7, 8:49*am, "brendan_gallag her_2...@yahoo. co.uk"
<brendan_galla gher_2...@yahoo .co.ukwrote:
>On Sep 7, 5:43*am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:


On 2008-09-06, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2...@yahoo. co.ukwrote:
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne twrote:
On 2008-09-05, brendan_gallagh er_2...@yahoo.c o.uk <brendan_gallag her_2...@yahoo. co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcD eleteThis.org.a uwrote:
brendan_gallag her_2...@yahoo. co.uk wrote:
Hi
I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. ?In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. ?Has anyone seen this
before? ?What can I do to prevent it?
? ? Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop
your service.
? ? ? ? Timer1.Enabled = False
? ? End Sub
? ? Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e
As System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVer bose Then
? ? ? ? ? ? Trace.WriteLine ("Executing DoSomething")
? ? ? ? End If
?
>Here's some code I use to do some work at 01:15 each night. It may give
you some ideas.
>Private mbBusy As Boolean
Private Timer1 As New System.Timers.T imer(60000)
>Private Sub Timer1_Elapsed( ByVal pSender As Object, ByVal pArgs As
System.Timers. ElapsedEventArg s)
>? ? ? ? If mbBusy Then Exit Sub
>? ? ? ? Dim z As DateTime
? ? ? ? Dim s As String = Format(z.Now, "HH:mm")
>? ? ? ? Select Case s
? ? ? ? ? ? ? ? Case "01:15"
>? ? ? ? ? ? ? ? ? ? ? ? mbBusy = True
? ? ? ? ? ? ? ? ? ? ? ? LogItem("BEGIN" )
>? ? ? ? ? ? ? ? ? ? ? ? DoJob()
>? ? ? ? ? ? ? ? ? ? ? ? LogItem("END")
? ? ? ? ? ? ? ? ? ? ? ? mbBusy = False
>? ? ? ? ? ? ? ? Case Else
? ? ? ? ? ? ? ? ? ? ? ? 'LogItem("Revie wService " & s)
>? ? ? ? End Select
>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.
>? ? ? ? LogItem("Review Service Started")
>? ? ? ? AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
? ? ? ? Timer1.Start()
? ? ? ? Timer1.Enabled = True
>End Sub
>Protected Overrides Sub OnStop()
? ? ? ? ' Add code here to perform any tear-down necessary to stop your service.
>? ? ? ? Timer1.Enabled = False
? ? ? ? Timer1.Stop()
? ? ? ? RemoveHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
>? ? ? ? LogItem("Review Service Stopped")
>End Sub
>LogItem just appends some text to a (log) file. DoJob does the work I
want done. It seems to work okay.
>HTH- Hide quoted text -
>- Show quoted text -
Thanks Jason. *Do you think there is anything I can do on the
threading side to prevent two threads executing the the DoSomething()
method at the same time?
>Yes, put in a lock...
>In your service class
>Private Shared _lock As New Object()
>In your DoSomething() Method:
>Private Sub DoSomething ()
* * * * SyncLock _lock ' only one thread at a time can enter here
* * * * * * * * ' Do your cool stuff
* * * * End SyncLock
End Sub
>--
Tom Shelton- Hide quoted text -
>- Show quoted text -
Thanks Tom. *Is there any chance of a deadlock happening when using
SyncLock in this way?
It depends on what you do inside the lock... *If for some reason, a thread
enters the lock, and never exits - then yes, you could deadlock.
--
Tom Shelton- Hide quoted text -
- Show quoted text -

thanks again. *Is there any way to avoid that happening i.e. kill the
thread after a certain lenght of time?- Hide quoted text -

- Show quoted text -

I have just discovered that the Monitor class resolves this:
Public Sub Foo()
Dim sText As String
Dim objLock As Object = New Object()
Try
Monitor.Enter(o bjLock)
Try
sText = "Hello"
Finally
Monitor.Exit(ob jLock)
End Try
Catch e As Exception
MessageBox.Show (e.Message)
End Try
End Sub
What you showed there is essentially what SyncLock does, only expanded :) In
other worrds, there is no difference between SyncLock and the code your wrote.
The only way Monitor helps you is that you can specify a timeout for the wait
on a lock.

Under normal circumstances, the use of a SyncLock as I originally posted is
perfectly adequate. You asked about deadlock, and indeed if you do something
like:

SyncLock _lock
SomeOtherMethod ()
End SycnLock

And that method hangs (for instance, and infinte loop) - then any threads that
try to enter the lock will hang. An exception will not cause this
situation, since SyncLock is essentially:

Try
Monitor.Enter(_ lock)
SomeOtherMethod ()
Finally
Monitor.Exite(_ lock)
End Try

If you need a timed lock, then you might want to look at this article by Ian
Griffiths - it explains how to create a timed lock using the C# using
statement, but it is easily ported to VB...

http://www.interact-sw.co.uk/iangblo.../03/23/locking

Again, this maybe overkill - if nothing inside of your synclock is likely to
hang, then code like this should be adequate:

Try
SycnLock _lock
' Do Cool Stuff
End SyncLock
Catch e As Exception
' handle your exception
End Catch

--
Tom Shelton
Sep 8 '08 #9
br************* *******@yahoo.c o.uk wrote:
Hi

I am seeing some strange behaviour on a windows (vb.net 1.1) service.
Basically, what I see happening is that when the Timer1_Elapsed event
fires, it attempts to execute Timer1.Stop() but takes a long time to
do this. In the meantime, the Timer1_Elapsed event fires again and a
new thread seems to try and also tries to execute Timer1.Stop().
Eventually, both threads manage to execute Timer1.Stop() and both
threads execute the DoSomething() method. Has anyone seen this
before? What can I do to prevent it?
Although wrestling with thread locking may get you a fix, maybe you should try
to determine
A. why is Timer1.Stop() taking a long time?
B. why is a second thread getting started on a second elapsed event?

Neither of those seem right to me.
Sep 9 '08 #10

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

Similar topics

2
15685
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. Thanks Andrew
0
2186
by: omyek | last post by:
Heh, I know some of you have read the subject and are wondering what in the heck I'm doing. Well, for all intensive purposes, I've had to embed a WebBrowser control into a DLL and reference that DLL in a windows service I've created. The problem is I get the following error/stacktrace: System.Threading.ThreadStateException: Could not...
2
8457
by: hnkien | last post by:
Hi, I am writing a windows service with threading.timer for 10 seconds but it didn't work. Here are my code: namespace SchedulerService { public class ScheduleService : System.ServiceProcess.ServiceBase { private System.ComponentModel.IContainer components; protected System.Threading.Timer tmrThreadingTimer;
7
1805
by: Doug Stiers | last post by:
I have a VB app that I'm installing as a Windows Service. I want a subroutine in the app to run every 30 minutes during business hours. How do I do this in VB? I set the startup type as automatic so it's always running. Is there some kind of timer event I can use? Thanks, Doug
2
4944
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web method may succeed one time and not another. I cannot find any reason why it would work one time and not another. The exception occurs every two...
28
7356
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). I have created a windows service from VStudio 2005 template. What is the windows service replacement for Application.Run()?
1
2865
by: mdhaman | last post by:
hi, I have a windows service written in VB.Net and framework 2.0. It is a multithread service and it is using threadpool to manage threads. Recently I have started getting NullReferenceException and after that windows service crashes. System.Transactions Critical: 0 : <TraceRecord...
4
8817
by: Steven De Smet | last post by:
Hello, This is my first post. I searched on the internet for answers but I was unable to solve my problem. So I hope that you guy's can help me with my VB.NET problem I tried to create a windows service that converts MS Word Files into .PDF files and after that we want to zip the .PDF files. Our code: Protected Overrides Sub...
8
3362
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location on a network for files and then copies these files to another location (on the network) AND then updates a record in the database. The file copying...
0
7912
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...
0
7839
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...
0
8202
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. ...
0
8338
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...
0
8216
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...
0
5390
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...
1
2345
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
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
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...

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.