Windows server OnTimedEvent
-
I have the following onTime event in windows server. I would like to try 3 times to connect and submit files before I throw an exception. How would I do this?
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Try aTimer.Stop() Dim fileName As String = String.Empty While queueList.Count > 0 If \_SFTP.Connected = False OrElse \_SFTP.SFTPState = Dart.Ssh.ConnectionState.Closed Then \_SFTP.Connect() End If fileName = queueList.Dequeue.ToString If Not fileName Is Nothing AndAlso fileName <> String.Empty Then ProcessFile(fileName) End If End While If \_SFTP.Connected = True Then \_SFTP.Disconnect() End If aTimer.Start() Catch ex As Exception modCommon.SendEmailExceptionToDev("OnTimed: OnTimedEvent Exception", ex) Err.Clear() End Try
-
I have the following onTime event in windows server. I would like to try 3 times to connect and submit files before I throw an exception. How would I do this?
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Try aTimer.Stop() Dim fileName As String = String.Empty While queueList.Count > 0 If \_SFTP.Connected = False OrElse \_SFTP.SFTPState = Dart.Ssh.ConnectionState.Closed Then \_SFTP.Connect() End If fileName = queueList.Dequeue.ToString If Not fileName Is Nothing AndAlso fileName <> String.Empty Then ProcessFile(fileName) End If End While If \_SFTP.Connected = True Then \_SFTP.Disconnect() End If aTimer.Start() Catch ex As Exception modCommon.SendEmailExceptionToDev("OnTimed: OnTimedEvent Exception", ex) Err.Clear() End Try
Remove the exception handler, wrap it in a new method, add exception handling there. Call your own function again from the exception-handler if it fails, and pass a counter. If the counter is three, don't call your own function, but accept defeat.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
Remove the exception handler, wrap it in a new method, add exception handling there. Call your own function again from the exception-handler if it fails, and pass a counter. If the counter is three, don't call your own function, but accept defeat.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
Here is what I did: 1.Add new sub: RetryOnTimeout 2. Add retry in Catch. Is this what you suggested ?
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
TryRetryOnTimeout(source, e) Catch ex As Exception While \_currentRetry < 3 RetryOnTimeout(source, e) \_currentRetry = \_currentRetry + 1 End While modCommon.SendEmailExceptionToDev("OnTimedEvent Exception", ex) Err.Clear()
End Try
End Sub Private Sub RetryOnTimeout(source As Object, e As ElapsedEventArgs) Try aTimer.Stop() Dim fileName As String = String.Empty While queueList.Count > 0 If \_SFTP.Connected = False OrElse \_SFTP.SFTPState = Dart.Ssh.ConnectionState.Closed Then \_SFTP.Connect() End If fileName = queueList.Dequeue.ToString If Not fileName Is Nothing AndAlso fileName <> String.Empty Then ProcessFile(fileName) End If End While If \_SFTP.Connected = True Then \_SFTP.Disconnect() End If aTimer.Start() Catch ex As Exception modCommon.SendEmailExceptionToDev("OnTimedEvent Exception", ex) Err.Clear() End Try End Sub
-
Here is what I did: 1.Add new sub: RetryOnTimeout 2. Add retry in Catch. Is this what you suggested ?
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
TryRetryOnTimeout(source, e) Catch ex As Exception While \_currentRetry < 3 RetryOnTimeout(source, e) \_currentRetry = \_currentRetry + 1 End While modCommon.SendEmailExceptionToDev("OnTimedEvent Exception", ex) Err.Clear()
End Try
End Sub Private Sub RetryOnTimeout(source As Object, e As ElapsedEventArgs) Try aTimer.Stop() Dim fileName As String = String.Empty While queueList.Count > 0 If \_SFTP.Connected = False OrElse \_SFTP.SFTPState = Dart.Ssh.ConnectionState.Closed Then \_SFTP.Connect() End If fileName = queueList.Dequeue.ToString If Not fileName Is Nothing AndAlso fileName <> String.Empty Then ProcessFile(fileName) End If End While If \_SFTP.Connected = True Then \_SFTP.Disconnect() End If aTimer.Start() Catch ex As Exception modCommon.SendEmailExceptionToDev("OnTimedEvent Exception", ex) Err.Clear() End Try End Sub
I had something in mind with recursion, but this should work too. Would be easy to check by putting that code in a console, and replacing the mail-sending code with one that throws an exception. If you can't see what happens, add some logging.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
I had something in mind with recursion, but this should work too. Would be easy to check by putting that code in a console, and replacing the mail-sending code with one that throws an exception. If you can't see what happens, add some logging.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
here my updated code. I have replace email with : Throw ex. Is that what you mean? I am not sure I am clear on what you have in mind. Could you share maybe the code? :
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
TryRetryOnTimeout(source, e) Catch ex As Exception While \_currentRetry < 3 RetryOnTimeout(source, e) \_currentRetry = \_currentRetry + 1 End While modCommon.SendEmailExceptionToDev("ASH File System Watcher Outgoing: OnTimedEvent Exception", ex) Err.Clear() End Try End Sub Private Sub RetryOnTimeout(source As Object, e As ElapsedEventArgs) Try aTimer.Stop() Dim fileName As String = String.Empty While queueList.Count > 0 If \_SFTP.Connected = False OrElse \_SFTP.SFTPState = Dart.Ssh.ConnectionState.Closed Then \_SFTP.Connect() End If fileName = queueList.Dequeue.ToString If Not fileName Is Nothing AndAlso fileName <> String.Empty Then ProcessFile(fileName) End If End While If \_SFTP.Connected = True Then \_SFTP.Disconnect() End If aTimer.Start() Catch ex As Exception Throw ex End Try End Sub
-
here my updated code. I have replace email with : Throw ex. Is that what you mean? I am not sure I am clear on what you have in mind. Could you share maybe the code? :
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
TryRetryOnTimeout(source, e) Catch ex As Exception While \_currentRetry < 3 RetryOnTimeout(source, e) \_currentRetry = \_currentRetry + 1 End While modCommon.SendEmailExceptionToDev("ASH File System Watcher Outgoing: OnTimedEvent Exception", ex) Err.Clear() End Try End Sub Private Sub RetryOnTimeout(source As Object, e As ElapsedEventArgs) Try aTimer.Stop() Dim fileName As String = String.Empty While queueList.Count > 0 If \_SFTP.Connected = False OrElse \_SFTP.SFTPState = Dart.Ssh.ConnectionState.Closed Then \_SFTP.Connect() End If fileName = queueList.Dequeue.ToString If Not fileName Is Nothing AndAlso fileName <> String.Empty Then ProcessFile(fileName) End If End While If \_SFTP.Connected = True Then \_SFTP.Disconnect() End If aTimer.Start() Catch ex As Exception Throw ex End Try End Sub
byka wrote:
I am not sure I am clear on what you have in mind
My bad; but you're complicating the problem by thinking about emails. In these cases its easier to start a new project and make a sketch of what you'd need. Example below, for a console-application.
Module Module1
Public Function MyEmailMethod() As Int32
' we throw an exception, to test the handling and the retrying thereof.
' in the actual application an email would be sent from here.
Throw New NotImplementedException("test")
End FunctionPublic Function TryMyEmailMethod(maxTries As Integer, Optional currentTry As Integer = 1) As Int32 If currentTry <= maxTries Then Try Console.WriteLine(String.Format("Attempt {0}", currentTry)) MyEmailMethod() Catch ex As Exception Console.WriteLine("Caught: " + ex.GetType().Name) TryMyEmailMethod(maxTries, currentTry + 1) End Try Else Console.WriteLine("I've tried enough, that damn server is ignoring me!") End If End Function Sub Main() TryMyEmailMethod(3) 'halt execution to show result Console.ReadKey() End Sub
End Module
That would result in below output if you run it, showing the principle works as intended;
Attempt 1
Caught: NotImplementedException
Attempt 2
Caught: NotImplementedException
Attempt 3
Caught: NotImplementedException
I've tried enough, that damn server is ignoring me!Your "NotImplementedException" would be the exception that the mail-routine is throwing, and you might want to log the lines where the above code is writing to the console; after all, if a mail-server stops responding, you'd like to verify that everything else worked as expected. Making a new project allows to focus on the problem, without anything else bothering. It also makes reuse of the new pattern somewhat easier, IMO. Also take not that above code could encounter three different exceptions or reasons why it cannot send mail, without it giving much info yet on why.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Remove the exception handler, wrap it in a new method, add exception handling there. Call your own function again from the exception-handler if it fails, and pass a counter. If the counter is three, don't call your own function, but accept defeat.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
wow make you crazy it was very disrespectful
-
wow make you crazy it was very disrespectful