473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.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
9 2143
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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. ...
0
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...
2
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 :...
7
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...
2
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...
28
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). ...
1
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...
4
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...
8
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.