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
… 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
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?
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
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?
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
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?
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
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 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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;
|
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
|
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...
| |
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()?
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |