Search code examples
asp.net.netvbavb.netthread-sleep

Thread.Sleep () with Timer for schedule


I have a select case of options which can run according to the set schedules. Therefore, I've written the code provided below.

Private WithEvents tmr_Imp_Pms_File As New Timers.Timer(950) 'for PMS File

Private Sub tmr_Imp_Pms_File_Elasped(ByVal sender As System.Object, ByVal e As Timers.ElapsedEventArgs) Handles tmr_Imp_Pms_File.Elapsed

Thread.Sleep(50)

     Select Case .Enable_Schedule
                Case 1 ' Schedule - Daily Option
                    If .Enable_Daily_Option_1 = 1 Then
                        If _
                            TimeSerial(Now.Hour, Now.Minute, Now.Second) =
                            TimeSerial(.Daily_Option_1_Time.Hour, .Daily_Option_1_Time.Minute,
                                       .Daily_Option_1_Time.Second) Then

                            logger.log.Info("Checking Running Daily Schedule Option 1, " & Now.ToString)

                        End If
                    End If


                    If .Enable_Daily_Option_2 = 1 Then
                        If _
                            TimeSerial(Now.Hour, Now.Minute, Now.Second) =
                            TimeSerial(.Daily_Option_2_Time.Hour, .Daily_Option_2_Time.Minute,
                                       .Daily_Option_2_Time.Second) Then

                            logger.log.Info("Checking Running Daily Schedule Option 2, " & Now.ToString)

                        End If
                    End If


                    If .Enable_Daily_Option_3 = 1 Then
                        If _
                            TimeSerial(Now.Hour, Now.Minute, Now.Second) =
                            TimeSerial(.Daily_Option_3_Time.Hour, .Daily_Option_3_Time.Minute,
                                       .Daily_Option_3_Time.Second) Then

                            logger.log.Info("Checking Running Daily Schedule Option 3, " & Now.ToString)

                        End If
                    End If
     End Select

        End Sub

Here I have 3 options of schedule, I enable all 3 schedules for my program to run. But there is a random and rarely happened issue where the program will skip the second option from running. I believe it is something to do with this part shown below.

 " TimeSerial(Now.Hour, Now.Minute, Now.Second) =
                        TimeSerial(.Daily_Option_1_Time.Hour, .Daily_Option_1_Time.Minute,
                                   .Daily_Option_1_Time.Second) Then"

How may I enhance this piece of code to solve the issue? Are there any other ways to write the code? "I know there are..." but can someone show me some example of a better way to handle this issue.

Appreciate your help, thanks in advance.


Solution

  • i found the answer, below is the way to fix the issue.

           Private WithEvents tmr_Imp_Pms_File As New Timers.Timer(950) 'for PMS File
    
           Private Sub tmr_Imp_Pms_File_Elasped(ByVal sender As System.Object, ByVal e As Timers.ElapsedEventArgs) Handles tmr_Imp_Pms_File.Elapsed
    
                Select Case .Enable_Schedule
                           Case 1 ' Schedule - Daily Option
                               If .Enable_Daily_Option_1 = 1 Then
                                   If _
                                       TimeSerial(Now.Hour, Now.Minute, Now.Second) =
                                       TimeSerial(.Daily_Option_1_Time.Hour, .Daily_Option_1_Time.Minute,
                                                  .Daily_Option_1_Time.Second) Then
    
    
                                   logger.log.Info("Daily Schedule Option 2 Hit, " & Now.ToString("HH:mm:ss.fff"))
    
                                   tmr_Imp_Pms_File.Stop() : Thread.Sleep(50)
    
                                   End If
                               End If
    
    
                               If .Enable_Daily_Option_2 = 1 Then
                                   If _
                                       TimeSerial(Now.Hour, Now.Minute, Now.Second) =
                                       TimeSerial(.Daily_Option_2_Time.Hour, .Daily_Option_2_Time.Minute,
                                                  .Daily_Option_2_Time.Second) Then
    
    
                                   logger.log.Info("Daily Schedule Option 2 Hit, " & Now.ToString("HH:mm:ss.fff"))
    
                                   tmr_Imp_Pms_File.Stop() : Thread.Sleep(50)
    
                                   End If
                               End If
    
    
                               If .Enable_Daily_Option_3 = 1 Then
                                   If _
                                       TimeSerial(Now.Hour, Now.Minute, Now.Second) =
                                       TimeSerial(.Daily_Option_3_Time.Hour, .Daily_Option_3_Time.Minute,
                                                  .Daily_Option_3_Time.Second) Then
    
    
                                   logger.log.Info("Daily Schedule Option 2 Hit, " & Now.ToString("HH:mm:ss.fff"))
    
                                   tmr_Imp_Pms_File.Stop() : Thread.Sleep(50)
    
                                   End If
                               End If
                End Select
    
                   End Sub
       ```