Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Windows server OnTimedEvent

Windows server OnTimedEvent

Scheduled Pinned Locked Moved Windows Forms
sysadminwindows-adminquestion
8 Posts 3 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    byka
    wrote on last edited by
    #1

    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
    
    L 1 Reply Last reply
    0
    • B byka

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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)

      B B 2 Replies Last reply
      0
      • L Lost User

        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)

        B Offline
        B Offline
        byka
        wrote on last edited by
        #3

        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)
        Try

                RetryOnTimeout(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
        
        L 1 Reply Last reply
        0
        • B byka

          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)
          Try

                  RetryOnTimeout(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
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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)

          B 1 Reply Last reply
          0
          • L Lost User

            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)

            B Offline
            B Offline
            byka
            wrote on last edited by
            #5

            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)
            Try

                    RetryOnTimeout(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
            
            L 1 Reply Last reply
            0
            • B byka

              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)
              Try

                      RetryOnTimeout(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
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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 Function

              Public 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[^]

              1 Reply Last reply
              0
              • L Lost User

                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)

                B Offline
                B Offline
                bogel bogel
                wrote on last edited by
                #7

                wow make you crazy it was very disrespectful

                L 1 Reply Last reply
                0
                • B bogel bogel

                  wow make you crazy it was very disrespectful

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  :laugh:

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups