Addhandler and AddressOf in .net



Hello Am正在尝试编写以下代码来激发MyHelper方法。但它并没有向timScheduledTask开火。Elapsed,
请任何人帮忙,激发MyHelper方法的最佳方法是什么?

 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

您的问题是timScheduledTaskApplication_Start函数的位置变量。一旦此函数完成,计时器的实例就会被垃圾收集器销毁。如果将计时器存储在类变量中,则应该可以。

例如:

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:AspNetSqlQuotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], onRemoveCallback)
    End Sub
End Class

甚至像这样:

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:AspNetSqlQuotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], onRemoveCallback)
    End Sub
End Class

代码必须是这样的
但最重要的是我们必须牢记。如果您将时间间隔设置为50秒,则每秒钟代码执行都应停止在起始标记线以下的断点处,即_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:AspNetSqlQuotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], _
         onRemoveCallback)**
        Return ds
    End Function
End

相关内容

  • 没有找到相关文章

最新更新