0

Hello Am trying to write the below code to fire the MyHelper method. But It is not firing at timScheduledTask.Elapsed,
Please could any one help, what is best method to fire MyHelper method?

 Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

        Dim timScheduledTask As New System.Timers.Timer
        timScheduledTask.Interval = 600 * 1000 //in milliseconds    
        timScheduledTask.Enabled = True
        timScheduledTask.Start()
    **AddHandler timScheduledTask.Elapsed, AddressOf MyHelper**    
    End Sub

    Protected Sub MyHelper(ByVal sender As Object, ByVal e As EventArgs)
        //Just Do something        
    End Sub
Software Enginner
  • 675
  • 2
  • 14
  • 43

2 Answers2

0

Your problem is that the timScheduledTask is a location variable to the Application_Start function. Once this function finishes, the instance of the timer is destroyed by the garbage collector. If you store the timer in a class variable you should be fine.

For example like this:

Public Class Foo
    Private timScheduledTask As System.Timers.Timer

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        timScheduledTask = New System.Timers.Timer
        timScheduledTask.Interval = 600 * 1000 //in milliseconds    
        timScheduledTask.Enabled = True
        timScheduledTask.Start()
        AddHandler timScheduledTask.Elapsed, AddressOf MyHelper   
    End Sub

    Protected Sub MyHelper(ByVal sender As Object, ByVal e As EventArgs)
        Dim ds As DataSet = obj1.Dataset()

        Dim onRemoveCallback As CacheItemRemovedCallback

        _cache.Insert("ResultDataset", ds, New CacheDependency("C:\AspNetSql\Quotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], onRemoveCallback)
    End Sub
End Class

Or even like this:

Public Class Foo
    Private WithEvents timScheduledTask As System.Timers.Timer

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        timScheduledTask = New System.Timers.Timer
        timScheduledTask.Interval = 600 * 1000 //in milliseconds    
        timScheduledTask.Enabled = True
        timScheduledTask.Start()  
    End Sub

    Protected Sub MyHelper(ByVal sender As Object, ByVal e As EventArgs) Handles timScheduledTask.Elapsed
        Dim ds As DataSet = obj1.Dataset()

        Dim onRemoveCallback As CacheItemRemovedCallback

        _cache.Insert("ResultDataset", ds, New CacheDependency("C:\AspNetSql\Quotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], onRemoveCallback)
    End Sub
End Class
Nitram
  • 6,486
  • 2
  • 21
  • 32
  • This is not true for an enabled timer. While it is an intuitive answer, there are factors with timers that make it incorrect http://stackoverflow.com/questions/4959149/do-timer-object-get-gc-ed-when-no-other-object-references-them – Marc Jul 18 '12 at 14:40
  • Ha. I did not know that. Okay. Then there still could be a problem that is caused by the onRemoveCallback being not initialized. But that can't be told for sure because I have no clue what the class of _cache is. – Nitram Jul 18 '12 at 14:47
  • @Nitram, I tried with your above two codes. we have to use Private WithEvents timScheduledTask As New System.Timers.Timer to avoid the error "System.NullReferenceException: Object reference not set to an instance of an object." at line timScheduledTask.Interval = 600 * 1000. – Software Enginner Jul 18 '12 at 14:50
  • @Nitram, Looks timScheduledTask is not Elapsing with my above code. Because. If timScheduledTask.Elapsed, it should fire the method when we used you second code. – Software Enginner Jul 18 '12 at 14:52
  • @OverflowUser, did you remove the line `timScheduledTask = New System.Timers.Timer` at your first try? It sounds this way if the variable is not set. Did you try writing some console output at the begin of your handler function to make sure that the problem is not caused by the _cache.Insert stuff? – Nitram Jul 18 '12 at 14:56
  • @Nitram, for a while please ignore the clause _cache, Problem with firing the method, once it fired we might have the problem with _cache. FYI.Public Shared _cache As New Cache. Thank You – Software Enginner Jul 18 '12 at 15:01
  • @Nitram, Please find the updated post. Now I have removed _cache.insert. – Software Enginner Jul 18 '12 at 15:03
  • 1
    Just a wild guess: Try replacing the `EventArgs` of your handler function with `ElapsedEventArgs`. – Nitram Jul 18 '12 at 15:05
  • @Nitram, tried with, Protected Sub MyHelper(ByVal sender As Object, ByVal e As ElapsedEventArgs) AND Protected Sub MyHelper(ByVal sender As Object, ByVal e As ElapsedEventArgs) Handles timScheduledTask.Elapsed Didn't work! Hmmmm – Software Enginner Jul 18 '12 at 15:09
  • @Nitram, This link might help us http://forums.asp.net/t/1752363.aspx/1 Thank you very much for being with me. – Software Enginner Jul 18 '12 at 15:15
  • In general it has to work with even with a very short timer interval. Like one millisecond. I used many of those timers and it worked. The only difference being that the handler functions are always private. But I doubt that this is the problem. – Nitram Jul 18 '12 at 15:25
  • @Nitram, Thank You Thank You Very Much for working with me on this issue. I resolved it with the below post answered code. I think your suggestions are correct, but I was failed to test the timer correctly. Anyway, below code working fine now. Thank You Very Much Again. – Software Enginner Jul 18 '12 at 17:37
0

Code has to looks like this.
But maing thing we have to keep in mind. If you set the intervel 50seconds, for every seconds code execution should stop at a break point at below start marked line i.e, _cache.insert.

Class Foo  
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

        //Caching the Dataset for every mentioned time interval
        _cache = Context.Cache
        Dim timer As New System.Timers.Timer()
        timer.Interval = 50 * 1000 //in milliseconds = 50Seconds   
        timer.Enabled = True

        //Automatically calling the GetMetricsDataset mothod for every mentioned time interval
        AddHandler timer.Elapsed, AddressOf Me.GetDataset

        timer.Stop()
        timer.Start()

    End Sub

    Private Function GetDataset(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) As DataSet
        Dim ds As DataSet = obj1.GetDataset()

        Dim onRemoveCallback As CacheItemRemovedCallback

        **_cache.Insert("Dataset", ds, New CacheDependency("C:\AspNetSql\Quotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], _
         onRemoveCallback)**

        Return ds
    End Function
End
Software Enginner
  • 675
  • 2
  • 14
  • 43