【问题标题】:No accessible 'New' accepts this number of arguments - Error没有可访问的“新”接受此数量的参数 - 错误
【发布时间】:2013-08-15 12:31:34
【问题描述】:

我有这段代码

Dim _timer As System.Threading.Timer

Public Sub RunTimer2()
    _timer = New System.Threading.Timer(onSave(), 
                         Nothing, 
                         TimeSpan.FromSeconds(5),     
                         TimeSpan.FromSeconds(5))
End Sub

我得到错误重载解析失败,因为没有可访问的“新”接受这个数量的参数。对于我试图创建计时器的行。虽然我可以在 msdn 和库中的文档中看到它具有我使用的这 4 种可能的类型参数。 没看懂……

【问题讨论】:

  • 将“onSave()”更改为“AddressOf onSave”

标签: vb.net visual-studio-2010


【解决方案1】:

您没有将委托传递给onSave 函数。您正在调用 onSave 函数并将其返回值传递给 Timer 构造函数。您需要为函数创建委托并传递它,如下所示:

Dim _timer As System.Threading.Timer

Public Sub RunTimer2()
    _timer = New System.Threading.Timer(New TimerCallback(AddressOf onSave), 
                         Nothing, 
                         TimeSpan.FromSeconds(5),     
                         TimeSpan.FromSeconds(5))
End Sub

或者,如果您这样做,VB 会自动为您找出委托类型:

Dim _timer As System.Threading.Timer

Public Sub RunTimer2()
    _timer = New System.Threading.Timer(AddressOf onSave, 
                         Nothing, 
                         TimeSpan.FromSeconds(5),     
                         TimeSpan.FromSeconds(5))
End Sub

【讨论】:

    【解决方案2】:

    要在 vb.net 中声明一个计时器,您可以:

      Private MyTimer As System.Threading.Timer
    
      MyTimer = New System.Threading.Timer(AddressOf MyTimer_Tick, Nothing, RunEveryNMinutes * 60000, RunEveryNMinutes * 60000)
    
    Private Sub MyTimer_Tick(ByVal state As Object)
        WriteEventLog("Timertick")
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      5 秒后启动计时器,每 5 秒调用一次

      Private timer As System.Threading.Timer = New System.Threading.Timer(AddressOf DoWhatever, Nothing, New TimeSpan(0, 0, 5, 0), New TimeSpan(0, 0, 5, 0))
      
      Private Sub dowhatever(sender As Object, e As Timers.ElapsedEventArgs)
         ' Do stuff
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-21
        • 1970-01-01
        • 2023-03-21
        • 2016-11-11
        相关资源
        最近更新 更多