By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,217 Members | 1,813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,217 IT Pros & Developers. It's quick & easy.

windows (vb.net 1.1) service - threading issue

P: n/a
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.ElapsedEventArgs) Handles Timer1.Elapsed
' stop the timer while the process runs
If Switch.TraceVerbose Then
Trace.WriteLine("Timer elapsed")
End If
Timer1.Stop()
DoSomething()
' restart the timer
Timer1.Start()
End Sub
Public Sub DoSomething()
If Switch.TraceVerbose Then
Trace.WriteLine("Executing DoSomething")
End If


Sep 5 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
br********************@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.ElapsedEventArgs) Handles Timer1.Elapsed
' stop the timer while the process runs
If Switch.TraceVerbose Then
Trace.WriteLine("Timer elapsed")
End If
Timer1.Stop()
DoSomething()
' restart the timer
Timer1.Start()
End Sub
Public Sub DoSomething()
If Switch.TraceVerbose 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.Timer(60000)

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

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("ReviewService " & 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("ReviewService 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("ReviewService 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

P: n/a
On Sep 5, 5:29*pm, Jason Keats <jke...@melbpcDeleteThis.org.auwrote:
brendan_gallagher_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, ByVale
As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
* * * * ' stop the timer while the process runs
* * * * If Switch.TraceVerbose Then
* * * * * * Trace.WriteLine("Timer elapsed")
* * * * End If
* * * * Timer1.Stop()
* * * * DoSomething()
* * * * ' restart the timer
* * * * Timer1.Start()
* * End Sub
* * Public Sub DoSomething()
* * * * If Switch.TraceVerbose 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.Timer(60000)

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

* * * * 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("ReviewService "& 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("ReviewService 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("ReviewService 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

P: n/a
On 2008-09-05, br********************@yahoo.co.uk <br********************@yahoo.co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcDeleteThis.org.auwrote:
>brendan_gallagher_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.ElapsedEventArgs) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVerbose Then
? ? ? ? ? ? Trace.WriteLine("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVerbose 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.Timer(60000)

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

? ? ? ? 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("ReviewService " & 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("ReviewService 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("ReviewService 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

P: n/a
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
On 2008-09-05, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2...@yahoo.co.ukwrote:


On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcDeleteThis.org.auwrote:
brendan_gallagher_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.ElapsedEventArgs) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVerbose Then
? ? ? ? ? ? Trace.WriteLine("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVerbose 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.Timer(60000)
Private Sub Timer1_Elapsed(ByVal pSender As Object, ByVal pArgs As
System.Timers.ElapsedEventArgs)
? ? ? ? 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("ReviewService " & 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("ReviewService 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("ReviewService 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

P: n/a
On 2008-09-06, br********************@yahoo.co.uk <br********************@yahoo.co.ukwrote:
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
>On 2008-09-05, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2...@yahoo.co.ukwrote:


On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcDeleteThis.org.auwrote:
brendan_gallagher_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.ElapsedEventArgs) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVerbose Then
? ? ? ? ? ? Trace.WriteLine("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVerbose 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.Timer(60000)
>Private Sub Timer1_Elapsed(ByVal pSender As Object, ByVal pArgs As
System.Timers.ElapsedEventArgs)
>? ? ? ? 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("ReviewService " & 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("ReviewService 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("ReviewService 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

P: n/a
On Sep 7, 5:43*am, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
On 2008-09-06, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2...@yahoo.co.ukwrote:


On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
On 2008-09-05, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2....@yahoo.co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcDeleteThis.org.auwrote:
brendan_gallagher_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 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.ElapsedEventArgs) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVerbose Then
? ? ? ? ? ? Trace.WriteLine("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVerbose 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.Timer(60000)
Private Sub Timer1_Elapsed(ByVal pSender As Object, ByVal pArgs As
System.Timers.ElapsedEventArgs)
? ? ? ? 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("ReviewService " & 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("ReviewService 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("ReviewService 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

P: n/a
On Sep 7, 8:49*am, "brendan_gallagher_2...@yahoo.co.uk"
<brendan_gallagher_2...@yahoo.co.ukwrote:
On Sep 7, 5:43*am, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:


On 2008-09-06, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2....@yahoo.co.ukwrote:
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
>On 2008-09-05, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2...@yahoo.co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcDeleteThis.org.auwrote:
>brendan_gallagher_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_Elapsedevent
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.ElapsedEventArgs) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVerbose Then
? ? ? ? ? ? Trace.WriteLine("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVerbose 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.Timer(60000)
>Private Sub Timer1_Elapsed(ByVal pSender As Object, ByVal pArgs As
>System.Timers.ElapsedEventArgs)
>? ? ? ? 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("ReviewService " & 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("ReviewService 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("ReviewService 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(objLock)
Try
sText = "Hello"
Finally
Monitor.Exit(objLock)
End Try
Catch e As Exception
MessageBox.Show(e.Message)
End Try
End Sub
Sep 8 '08 #8

P: n/a
On 2008-09-08, br********************@yahoo.co.uk <br********************@yahoo.co.ukwrote:
On Sep 7, 8:49*am, "brendan_gallagher_2...@yahoo.co.uk"
<brendan_gallagher_2...@yahoo.co.ukwrote:
>On Sep 7, 5:43*am, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:


On 2008-09-06, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2...@yahoo.co.ukwrote:
On Sep 5, 6:25*pm, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
On 2008-09-05, brendan_gallagher_2...@yahoo.co.uk <brendan_gallagher_2...@yahoo.co.ukwrote:
On Sep 5, 5:29?pm, Jason Keats <jke...@melbpcDeleteThis.org.auwrote:
brendan_gallagher_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.ElapsedEventArgs) Handles Timer1.Elapsed
? ? ? ? ' stop the timer while the process runs
? ? ? ? If Switch.TraceVerbose Then
? ? ? ? ? ? Trace.WriteLine("Timer elapsed")
? ? ? ? End If
? ? ? ? Timer1.Stop()
? ? ? ? DoSomething()
? ? ? ? ' restart the timer
? ? ? ? Timer1.Start()
? ? End Sub
? ? Public Sub DoSomething()
? ? ? ? If Switch.TraceVerbose 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.Timer(60000)
>Private Sub Timer1_Elapsed(ByVal pSender As Object, ByVal pArgs As
System.Timers.ElapsedEventArgs)
>? ? ? ? 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("ReviewService " & 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("ReviewService 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("ReviewService 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(objLock)
Try
sText = "Hello"
Finally
Monitor.Exit(objLock)
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

P: n/a
br********************@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?
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 discussion thread is closed

Replies have been disabled for this discussion.