I have a VS 2012 project that has a timer that kicks off a process every 2 minutes. It works fine but it seems to keep running after the project has been stopped. The only indication of this that I have is that it is logging errors to the database at the timer interval. Do I need to explicitly stop the timer? How is that done when you hit the stop button? When I close the project the logging stops.
The tick event (RefreshBroadcast) fails silently (when the app is not running) but the logging works fine, also the logging indicates that it does not have access to the config file as it uses the defaults (for application name etc). The logging code also is supposed to show the error in a View, so eventually Visual studio will tell me that my error view window “Does not have a resource identified by …”. This will happen with the project stopped, while I am coding somewhere else. After this the logging to the DB stops.
Public Sub New()
If Not System.Windows.Application.Current.MainWindow Is Nothing Then
_refreshBroadcastTimer = New System.Windows.Threading.DispatcherTimer()
AddHandler _refreshBroadcastTimer.Tick, AddressOf dispatcherTimer_Tick
If Debugger.IsAttached Then
_refreshBroadcastTimer.Interval = New TimeSpan(0, 0, 30)
Else
_refreshBroadcastTimer.Interval = New TimeSpan(0, 0, My.Settings.RefreshBroadcastIntervalMinutes * 60) ' change to seconds
End If
_refreshBroadcastTimer.Start()
End If
End Sub
Private Sub dispatcherTimer_Tick(sender As Object, e As EventArgs)
Try
RefreshBroadcast()
Catch ex As Exception
LogException(ex)
End Try
End Sub
Its funny how asking a question often gives you the answer, this has been bugging me for days but I think I have it figured. Because i have the timer starting when the view model is newed up, whenever I open the View in the editor it creates an instance of the view model in the background and this starts off the timer. I guess i have to wrap the code so it does not fire in the designer.
I have wrapped the init code in
it seems to be working so far.