[英]No accessible 'New' accepts this number of arguments - Error
我有這段代碼
Dim _timer As System.Threading.Timer
Public Sub RunTimer2()
_timer = New System.Threading.Timer(onSave(),
Nothing,
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(5))
End Sub
我得到錯誤重載解析失敗,因為沒有可訪問的“ New”接受此數目的參數。 對於我試圖創建計時器的行。 雖然我可以在msdn和庫中的文檔中看到它具有我使用的4種可能的類型參數。 我不明白......
您沒有將委托傳遞給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
要在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
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.