以下代碼-scheduler.vb-模擬使用ASP.NET運行計劃作業的Windows服務。更多的信息在這裏:http://beckelman.net/post/2008/09/20/Simulate-a-Windows-Service-Using-ASPNET-to-Run-Scheduled-Jobs.aspx調度程序代碼中的問題
Howerver當我嘗試運行我的global.asax中的類我得到以下錯誤(在RunScheduledTasks突出顯示):「表達式不會產生一個值」,爲什麼?謝謝。
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
任何意見可以理解!!!!!!!!! – netNewbi3 2010-12-02 14:30:19
你說它在'RunScheduledTasks'上突出顯示。什麼是突出顯示? Public Sub RunScheduledTasks()'? – Justin 2010-12-02 14:33:52
如果錯誤出現在'RunScheduledTasks'中,爲什麼你給我們的代碼只有'RunScheduledTasks'? – jason 2010-12-02 14:37:04