.net中的Addhandler和AddressOf

时间:2012-07-18 14:21:04

标签: .net addhandler

你好我试着编写下面的代码来解雇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

2 个答案:

答案 0 :(得分:0)

您的问题是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:\AspNetSql\Quotes.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:\AspNetSql\Quotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], onRemoveCallback)
    End Sub
End Class

答案 1 :(得分:0)

代码必须如此。
但是我们必须记住这件事。如果你设置了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:\AspNetSql\Quotes.Quotations"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.[Default], _
         onRemoveCallback)**

        Return ds
    End Function
End