简体   繁体   English

没有可访问的'New'接受这个数量的参数 - 错误

[英]No accessible 'New' accepts this number of arguments - Error

I have this chunk of code 我有这段代码

Dim _timer As System.Threading.Timer

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

and I get error Overload resolution failed because no accessible 'New' accepts this number of arguments. 我得到错误重载解析失败,因为没有可访问的“ New”接受此数目的参数。 for the line where I am trying to create the timer. 对于我试图创建计时器的行。 Although I can see in the documentation in msdn and in the libraries that it has those 4 possible types parameters that I use. 虽然我可以在msdn和库中的文档中看到它具有我使用的4种可能的类型参数。 I don't get it... 我不明白......

You are not passing a delegate to the onSave function. 您没有将委托传递给onSave函数。 You are calling the onSave function and passing it's return value to the Timer constructor. 您正在调用onSave函数,并将其返回值传递给Timer构造函数。 You need to create the delegate to the function and pass that, like this: 您需要创建该函数的委托并传递它,如下所示:

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

Or, VB will automatically figure out the delegate type for you if you just do this: 或者,如果您这样做,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

To declare a timer in vb.net you can: 要在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

That starts the timer after 5 seconds and calls it every 5 seconds 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有可访问的“ New”接受此数目的参数 - No accessible 'New' accepts this number of arguments 重载解析失败,因为没有可访问的“新”接受此数目的参数 - overload resolution failed because no accessible 'new' accepts this number of arguments 尝试将stringbuilder文本写入批处理文件时,“由于没有可访问的'New'接受此数目的参数,所以过载解析失败” - “Overload resolution failed because no accessible 'New' accepts this number of arguments” when trying to write stringbuilder text into a batch file VBNET重载,因为没有可访问的“ int”接受数字参数 - VBNET Overload because no accessible 'int' accepts number arguments 重载解析失败,因为没有可访问的“ ExecuteScalar”接受此数量的参数 - Overload resolution failed because no accessible 'ExecuteScalar' accepts this number of arguments 重载解析失败,因为没有可访问的'writealltext'接受此数量的参数 - overload resolution failed because no accessible 'writealltext' accepts this number of arguments 重载解析失败,因为没有可访问的“参数”接受此数量的 arguments - Overload resolution failed because no accessible 'Parameters' accepts this number of arguments VB.NET MVC重载解析失败,因为没有可访问的“创建”接受此数量的参数 - VB.NET MVC Overload resolution failed because no accessible 'Create' accepts this number of arguments 重载解析失败,因为对于这些参数,没有可访问的“新”是最具体的 - Overload resolution failed because no accessible 'new' is most specific for these arguments 重载解析失败,因为没有可访问的“新”是这些参数最具体的: - Overload resolution failed because no accessible 'New' is most specific for these arguments:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM