UltimateTimer - C#到VB.net转换&开始/结束语法错误?

时间:2014-10-13 01:56:35

标签: vb.net excel timer

尝试Tim Lovell-Smiths的UltimateTimer项目。

http://blogs.msdn.com/b/tilovell/archive/2014/01/29/a-light-weight-net-threadpool-timer-class.aspx

http://blogs.msdn.com/b/tilovell/archive/2014/01/31/sample-using-ultimatetimer-threadpooltimer.aspx

尝试将他的C#转换为VB.net,但他在Sub Main Line 2中的函数(lambda ??)是不正确的翻译/语法? - “表达不产生价值”

1)如何修复VB.net中Sub Main的第二行中的功能错误

2)我的代码是否正确设置为从SheetBeforeDoubleClick事件启动计时器?

3)我的代码是否已正确设置以从SheetBeforeDoubleClick事件中结束计时器?

Imports Microsoft.Office.Interop.Excel

Imports Microsoft.Office.Core

Imports ExcelDna.Integration

Imports System.Threading

Imports UltimateTimer

Public Class AddIn

    WithEvents Application As Application

    Shared timer As ThreadPoolTimer

    Private Shared Sub Main(ByVal args() As String)
        'ERROR NEXT LINE "EXPRESSION DOES NOT PRODUCE A VALUE" - OnTimer(timer)
        timer = ThreadPoolTimer.Create(Function() OnTimer(timer))  
        timer.SetTimer(DateTime.Now.AddSeconds(3), msPeriod:=0, acceptableMsDelay:=0)
        Console.WriteLine("Press any key to stop timer")
        Console.ReadLine()
        timer.Dispose()
    End Sub

    Private Shared Sub OnTimer(timer As ThreadPoolTimer)
        Console.WriteLine("Timer was called back! Resetting timer. The time is now " & DateTime.Now.ToString())
        timer.SetTimer(DateTime.Now.AddSeconds(3), 0, 0)
    End Sub

    Private Shared Sub Endtimer(timer As ThreadPoolTimer)
        timer.Dispose()
    End Sub

    Private Sub Application_SheetBeforeDoubleClick(Sh As Object, Target As Range, ByRef Cancel As Boolean) Handles Application.SheetBeforeDoubleClick
        If Target.Address = "$A$1" Then
            OnTimer(timer)
            MsgBox("TIME Is On My Side!!", , "Yeppers")
        ElseIf Target.Address = "$Z$1" Then
            Endtimer(timer)
            MsgBox("I Stopped TIME !!", , "Oh-Ho")
        End If
    End Sub

End Class

谢谢......:o)

编辑:2014年10月12日

好的,在Dave D的帮助下,我让Tim的UltimateTimer在vb.net中为我工作,并作为Excel ExcelDNA xLL AddIn的一部分。

以下是Imports,代码和示例框架(以及我用于我的xLL AddIN测试的其他代码),这个计时器可以在打包的xLL中使用以供Excel使用。

我通过双击来调用它,但可以通过右键菜单调用...等等。

Public Module MyFunctions

<ExcelFunction(Description:="My first .NET function")> _
Public Function dnaHello(name As String) As String
    Return "Hello " & name
End Function

结束模块

Public Class AddIn

Implements IExcelAddIn

WithEvents Application As Application
WithEvents Button As CommandBarButton

Public Sub AutoOpen() Implements IExcelAddIn.AutoOpen
    Application = ExcelDnaUtil.Application

    ' Add Cell context menu
    Dim ContextMenu As CommandBar

    ContextMenu = Application.CommandBars("Cell")
    Button = ContextMenu.Controls.Add(Type:=MsoControlType.msoControlButton, Before:=ContextMenu.Controls.Count, Temporary:=True)
    With Button
        .Caption = "Excel-DNA Test Button"
        .Tag = "EXCEL-DNA-Test"
    End With
End Sub

Public Sub AutoClose() Implements IExcelAddIn.AutoClose
    Button.Delete()
End Sub

Shared timer As ThreadPoolTimer

Private Shared Sub TimerMain()                      
    timer = ThreadPoolTimer.Create(Sub() OnTimer(timer))
    timer.SetTimer(DateTime.Now.AddSeconds(3), msPeriod:=0, acceptableMsDelay:=0)
End Sub

Private Shared Sub OnTimer(timer As ThreadPoolTimer)
    MsgBox("TIME Is On My Side!!" & DateTime.Now.ToString(), , "Yeppers")
    timer.SetTimer(DateTime.Now.AddSeconds(3), 0, 0)
End Sub

Private Shared Sub Endtimer(timer As ThreadPoolTimer)
    timer.Dispose()
End Sub

Private Sub Application_SheetBeforeDoubleClick(Sh As Object, Target As Range, ByRef Cancel As Boolean) Handles Application.SheetBeforeDoubleClick
    If Target.Address = "$A$1" Then
        MsgBox("TIME Is On My Side!!", , "Yeppers")
        TimerMain()
    ElseIf Target.Address = "$E$1" Then
        Endtimer(timer)
        MsgBox("I Stopped TIME !", , "Oh-Ho")
    End If
End Sub

Private Sub Button_Click(Ctrl As CommandBarButton, ByRef CancelDefault As Boolean) Handles Button.Click
    Application.StatusBar = "Excel-DNA Test Button - Clicked!"
End Sub

结束班

0 个答案:

没有答案