Question

The following code -scheduler.vb- simulate a Windows Service Using ASP.NET to Run Scheduled Jobs. More info here: http://beckelman.net/post/2008/09/20/Simulate-a-Windows-Service-Using-ASPNET-to-Run-Scheduled-Jobs.aspx

Howerver when I try to run the class in my global.asax i get the following error (highlighted on RunScheduledTasks): "Expression does not produce a value", why?? Thanks.

global.asax

 Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Scheduler.Run("test", 1, RunScheduledTasks)
End Sub


 Public Sub RunScheduledTasks()
 'Do stuff here
 end Sub

scheduler.vb

 Public Class Scheduler
Private Class CacheItem
    Public Name As String
    Public Callback As Callback
    Public Cache As Cache
    Public LastRun As DateTime
End Class

Public Delegate Sub Callback()

Private Shared _numberOfMinutes As Integer = 1

Public Shared Sub Run(ByVal name As String, ByVal minutes As Integer, ByVal callbackMethod As Callback)
    _numberOfMinutes = minutes

    Dim cache As New CacheItem()
    cache.Name = name
    cache.Callback = callbackMethod
    cache.Cache = HttpRuntime.Cache
    cache.LastRun = DateTime.Now
    AddCacheObject(cache)
End Sub

Private Shared Sub AddCacheObject(ByVal cache_1 As CacheItem)
    If cache_1.Cache(cache_1.Name) Is Nothing Then
        cache_1.Cache.Add(cache_1.Name, cache_1, Nothing, DateTime.Now.AddMinutes(_numberOfMinutes), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, _
         AddressOf CacheCallback)
    End If
End Sub

Private Shared Sub CacheCallback(ByVal key As String, ByVal value As Object, ByVal reason As CacheItemRemovedReason)
    Dim obj_cache As CacheItem = DirectCast(value, CacheItem)
    If obj_cache.LastRun < DateTime.Now Then
        If obj_cache.Callback IsNot Nothing Then
            obj_cache.Callback.Invoke()
        End If
        obj_cache.LastRun = DateTime.Now
    End If
    AddCacheObject(obj_cache)
End Sub
End Class
Was it helpful?

Solution

Looking at the link you provided, it looks like this may be an incorrect conversion from C# to VB. The C# code automatically creates a Callback delegate with RunScheduledTasks as its target to pass to Scheduler.Run.

I suspect that your VB.Net code is looking at this line:

Scheduler.Run("test", 1, RunScheduledTasks)

and expects RunScheduledTasks to be a function returning a Callback delegate. Unfortunately, I don't know VB.Net, so I'm guessing here. Maybe it would work if you added the AddressOf operator (link), like so:

Scheduler.Run("test", 1, AddressOf RunScheduledTasks)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top