SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help With .NET Caching

    Im making a program that gathers data from various places and then stores it within a database. I was looking for a way of making the code run itself every X mins/hours etc (to the point that I was even thinking of making a program to run locally and update the database) Im on a shared hosting account so my options were limited.

    Anyway I can across the following code....

    Code:
    Public Class Scheduler
    
    Private Class CacheItem
    Public Name As String
    Public Callback As Scheduler.Callback
    Public Expires As Date
    Public Cache As Caching.Cache
    Public LastRun As Date
    End Class
    
    Public Delegate Sub Callback()
    
    Private Shared _NumberOfMinutes As Integer = 5 'new
    
    Public Shared Sub Run(ByVal vstr_name As String, ByVal vint_minutes As Integer, ByVal vobj_call As Callback)
    _NumberOfMinutes = vint_minutes 'new
    Run(vstr_name, Date.Now.AddMinutes(vint_minutes), vobj_call)
    End Sub
    
    Public Shared Sub Run(ByVal vstr_name As String, ByVal vobj_expires As Date, ByVal vobj_call As Callback)
    Dim obj_cache As New CacheItem
    obj_cache.Name = vstr_name
    obj_cache.Callback = vobj_call
    obj_cache.Expires = vobj_expires
    obj_cache.Cache = HttpRuntime.Cache
    obj_cache.LastRun = Date.Now
    Run(obj_cache)
    End Sub
    
    Private Shared Sub Run(ByVal vobj_cache As CacheItem)
    If vobj_cache.Cache.Item(vobj_cache.Name) Is Nothing Then
    'vobj_cache.Cache.Add(vobj_cache.Name, vobj_cache, Nothing, vobj_cache.Expires, Nothing, CacheItemPriority.NotRemovable, AddressOf CacheCallback)
    vobj_cache.Cache.Add(vobj_cache.Name, vobj_cache, Nothing, Date.Now.AddMinutes(_NumberOfMinutes), Nothing, CacheItemPriority.NotRemovable, AddressOf CacheCallback) 'new
    End If
    End Sub
    
    Private Shared Sub CacheCallback(ByVal key As String, ByVal value As Object, ByVal reason As System.Web.Caching.CacheItemRemovedReason)
    Dim obj_cache As CacheItem = DirectCast(value, CacheItem)
    If obj_cache.LastRun < Date.Now Then
    If obj_cache.Callback IsNot Nothing Then
    obj_cache.Callback.Invoke()
    End If
    obj_cache.LastRun = Date.Now
    End If
    Run(obj_cache)
    End Sub
    
    End Class
    Which I call....

    Code:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    TestSub()
    Scheduler.Run("TEST", 5, AddressOf TestSub)
    
    End Sub
    Anyway it works pretty perfectly - except I cant seem to get it to stop, if I try "HttpRuntime.Cache.Remove("TEST")" then it will just start the scheduled task again. Anyone have any ideas on how I can stop this?

    I basically want it so that when I click let says Button2 then it will stop the schedule process.
    Turn My Text - íspuǝıɹɟ ʞooqǝɔɐɟ ɹnoʎ ǝzɐɯɐ puɐ ǝsnɟuoɔ

    Computer Game Cheats & Codes

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio, USA
    Posts
    1,494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you should really be using is a Timer object that is static (shared), and have a button's event handler call the Timer's Stop() method.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •