Pergunta

I am trying to call a function in one class from another on a Timer.Elapsed. I can call test functions just fine, but when I call the actual function I want, I get no exceptions, but the function just doesn't run. I have tried adding in some error handling (Catch ex) and outputting frequent messages to a .txt file to see where it fails, but I am not getting any of these messages in my log when I know that the function I am using to write these messages to the log is working.

How can I find where my function contains an error if I have no access to error messages?

Adding my code below - this is my write to log function.

Public Shared Function Output_To_Log(ByVal message As String) As String

    Dim strDate As String = Now.ToString("dd MMM yy HH:mm:ss ")
    Dim strTodayDate As String = Now.ToString("yyyyMMMdd")

    Dim file As New FileStream("C:\PHJones_Windows_Service\logs\Log" & strTodayDate & ".txt", FileMode.Append, FileAccess.Write)
    Dim stream As New StreamWriter(file)
    stream.WriteLine(message & " : " & strDate)
    stream.Close()

    Return ""

End Function

This is my Timer elapsed function.

Private Shared Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

    Output_To_Log("Working")

    PHJones.Start_Batch()

End Sub

This is my Start_Batch function, with references to my server blanked out with ****

Public Shared Function Start_Batch() As Integer
    Try

        Dim a As New running_check
        a.check = 1

        Dim files As String()
        Dim File As String
        Dim myProcess As New Diagnostics.Process()
        Dim File_Name As String
        Dim Running_FileName As String

        RunTimer.Output_To_Log("Start_Batch starting")

Start:
        RunTimer.Output_To_Log("Checking logs")
        Dim log_check As Integer = check_logs()

        RunTimer.Output_To_Log("Getting .DAT files.")
        files = IO.Directory.GetFiles("****\phjones\to_phjones\", "*.DAT")

        If files.Count > 0 Then
            RunTimer.Output_To_Log("Counted " & files.Count & " files.")
        Else
            RunTimer.Output_To_Log("No files found.")
        End If

        For Each File In files
            Try
                RunTimer.Output_To_Log("Starting process for " & File)
                Running_FileName = File & ".BAT"
                RunTimer.Output_To_Log("Processing " & Running_FileName)

                File_Name = File.Substring(26)

                If System.IO.File.Exists("C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT") Then
                    RunTimer.Output_To_Log("C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT already exists")
                Else
                    RunTimer.Output_To_Log("Copying " & Running_FileName & " to batch_files folder")
                    System.IO.File.Copy(Running_FileName, "C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT", True)
                End If

                If (System.IO.File.Exists("C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT")) Then
                    If (System.IO.File.Exists(Running_FileName)) Then
                        RunTimer.Output_To_Log("Deleting file " & Running_FileName)
                        System.IO.File.Delete(Running_FileName)
                    Else
                        RunTimer.Output_To_Log(File_Name & ".BAT does not exist in ****\phjones\to_phjones\processed")
                    End If
                Else
                    RunTimer.Output_To_Log(File_Name & ".BAT failed to copy")
                    Throw New Exception(File_Name & ".BAT failed to copy to C:\PHJones_Windows_Service\Batch_Files")
                End If

                RunTimer.Output_To_Log("Executing batch file " & Running_FileName)
                myProcess.StartInfo.UseShellExecute = True
                myProcess.StartInfo.FileName = "C:\PHJones_Windows_Service\Batch_Files\" & File_Name & ".BAT"
                myProcess.StartInfo.CreateNoWindow = False
                myProcess.Start()
                myProcess.WaitForExit()

                If System.IO.File.Exists("****\phjones\to_phjones\" & File_Name) Then
                    RunTimer.Output_To_Log("****\phjones\to_phjones\" & File_Name & " already exists")
                    System.IO.File.Delete(File)
                    RunTimer.Output_To_Log(File & " has been deleted")
                Else
                    RunTimer.Output_To_Log("Moving " & File)
                    System.IO.File.Move(File, "****\phjones\to_phjones\" & File_Name)
                End If

                Dim IWCnn = New OracleConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)

                Dim intRepair_Id As Integer = Mid(File_Name, 1, 7)
                Dim intRepair_seq As Integer = Mid(File_Name, 8, 1)

                RunTimer.Output_To_Log("Updating database for file " & File)
                IWCnn.Open()
                Dim StatusCmd As New OracleCommand("update works_orders " & _
                                    "set wor_sco_code = 'ISS', wor_issued_datetime = sysdate" & _
                                    " where wor_srq_no = " & intRepair_Id & _
                                    " and wor_seqno  = " & intRepair_seq, IWCnn)
                StatusCmd.ExecuteNonQuery()
                IWCnn.Close()

            Catch ex As Exception

                RunTimer.Timer1.Enabled = False

                RunTimer.Output_To_Log("Exception thrown in PHJones 2010 - " & ex.Message)



                Thread.Sleep(900000)
                RunTimer.Timer1.Enabled = True

                a.check = 0

                Return 0

            End Try
        Next

        a.check = 0

    Catch ex As Exception
        RunTimer.Output_To_Log("Exception thrown in PHJones 2010 - " & ex.Message)
    End Try


    Return 0


End Function

The entire RunTimer class.

Imports System.Configuration.ConfigurationSettings
Imports System.Data
Imports System.IO
Imports System.Diagnostics
Imports System
Imports System.Timers
Imports System.Threading
Imports System.ServiceProcess
Imports System.Configuration.Install


Public Class RunTimer

Inherits System.ServiceProcess.ServiceBase

Friend Shared WithEvents Timer1 As System.Timers.Timer

Public Counter As Integer = 0

Public Sub New()

    MyBase.New()
    InitializeComponents()

End Sub



Private Sub InitializeComponents()

    Me.ServiceName = "RunTimer"
    Me.AutoLog = True
    Me.CanStop = True
    Timer1 = New System.Timers.Timer()
    Timer1.Interval = 15000
    Timer1.Enabled = True

End Sub



' This method starts the service.

<MTAThread()> Shared Sub Main()

    ' To run more than one service you have to add them to the array

    System.ServiceProcess.ServiceBase.Run(New System.ServiceProcess.ServiceBase() {New RunTimer})

End Sub



' Clean up any resources being used.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

    MyBase.Dispose(disposing)

    ' TODO: Add cleanup code here (if required)

End Sub



Protected Overrides Sub OnStart(ByVal args() As String)

    ' TODO: Add start code here (if required) to start your service.

    Timer1.Enabled = True

End Sub



Protected Overrides Sub OnStop()

    ' TODO: Add tear-down code here (if required) to stop your service.
    Timer1.Enabled = False
    Output_To_Log("Ended")

End Sub


Private Sub InitializeComponent()


    Timer1 = New System.Timers.Timer

    CType(Timer1, System.ComponentModel.ISupportInitialize).BeginInit()

    Timer1.Enabled = True

    CType(Timer1, System.ComponentModel.ISupportInitialize).EndInit()

End Sub

Private Shared Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

    Output_To_Log("Working")

    PHJones.Start_Batch()

End Sub
Foi útil?

Solução

Since you're running as a service, you won't see ordinary error messages. It's possible there is an error between Output_To_Log("Working") in the Timer event and RunTimer.Output_To_Log("Start_Batch starting") of Start_Batch(). For example, if an error could occur in the initialization of Dim a As New running_check, or in the call itself, PHJones.Start_Batch(). Either of these would cause what you're seeing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top