2010-11-22 77 views
0

以下代碼-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 
+0

任何意見可以理解!!!!!!!!! – netNewbi3 2010-12-02 14:30:19

+0

你說它在'RunScheduledTasks'上突出顯示。什麼是突出顯示? Public Sub RunScheduledTasks()'? – Justin 2010-12-02 14:33:52

+0

如果錯誤出現在'RunScheduledTasks'中,爲什麼你給我們的代碼只有'RunScheduledTasks'? – jason 2010-12-02 14:37:04

回答

0

看着你提供的鏈接,它看起來像這可能是從C#不正確的轉換到VB。 C#代碼自動創建一個Callback代表,RunScheduledTasks作爲其目標傳遞到Scheduler.Run

我懷疑你的VB.Net代碼看着這行:

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

,預計RunScheduledTasks是返回Callback委託的功能。不幸的是,我不知道VB.Net,所以我在這裏猜測。也許如果你添加的AddressOf運營商(link)這是可行的,就像這樣:

Scheduler.Run("test", 1, AddressOf RunScheduledTasks) 
+0

非常感謝。錯誤消失了。對於基本問題抱歉,但我對代表不熟悉。再次感謝。 – netNewbi3 2010-12-02 15:04:08