Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

Windows Service To Parse and Move Files

Hi Experts,

I am new to VB.net and I am trying to create a service that will move files from one directory to another where it will be parsed and imported into our EDM system by a third party import engine.  Each file needs be processed by the import engine sequentially.  

The system that sends the files sends them in batches in no particular order.  The import engine doesn't process files in any particular order either.  Each file is named like so: nnn-nnnnnnnnnn-XXXX-nnnnnnnnnnnnnn.pdf, where n = numbers and X = a document type (DIMP, DIMF, DIPT, DIPP).  The problem is DIMPs need to be processed before DIMFs, so normal sorts don't work.  I have developed the code to test in a normal windows form application which runs like i had hoped.  When the button is clicked everything is parsed and moved correctly.  

However, when run as a service the program sometimes moves one, sometimes 2 or 3, and then eventually errors out with a file not found error.  Every time the file has already been moved and the program tries to move it again causing the exception to occur. When I debug and press F8 to step through it appears to execute the loops in a random sequence. Which is causing even more confusion

Any help would be greatly appreciated!! I have read through numerous user posts, blogs, news groups, and documentation and no luck.

I am new to vb.net so please forgive any weird coding techniques :)
Imports System.Threading
Imports System.Net.Mail
Imports System.IO.File
 
Public Class CCS_DI_Monitor
    Private oTimer As System.Threading.Timer
 
    Protected Overrides Sub OnStart(ByVal args() As String)
        Dim oCallback As New TimerCallback(AddressOf OnTimedEvent)
        oTimer = New System.Threading.Timer(oCallback, Nothing, 30000, 30000)
    End Sub
 
    Protected Overrides Sub OnStop()
       
    End Sub
 
    Private Sub OnTimedEvent(ByVal state As Object)
        Dim strOrgPath As String = "C:\Temp\CCS_DITest\"
        Dim strDestPath As String = "C:\Temp\CCS_DITest\CCS_DI_Import\"
        Dim intFileCount As Integer = 0
        Dim strFileName As String = ""
        Dim strFileVer As String = ""
        Dim strDocType As String = ""
        Dim strAcctNum As String = ""
        Dim strClinID As String = ""
        Dim sortFileName As String = ""
        Dim i As Integer = 0
        Dim arrFileName(i) As String
        Dim strDestFile As String = ""
        Dim strOrgFile As String
 
 
        Try
            intFileCount = System.IO.Directory.GetFiles(strOrgPath).Length
 
            If intFileCount >= 1 Then
                For Each file In System.IO.Directory.GetFiles(strOrgPath)
                    strFileName = System.IO.Path.GetFileName(file)
                    strFileVer = Mid(strFileName, 21, 14)
                    strDocType = Mid(strFileName, 16, 4)
                    strAcctNum = Mid(strFileName, 5, 10)
                    strClinID = LSet(strFileName, 3)
                    If strDocType = "DIMP" Then
                        strDocType = "DIMC"
                    End If
                    sortFileName = strClinID & strFileVer & strAcctNum & strDocType
 
                    ReDim Preserve arrFileName(i)
                    arrFileName(i) = sortFileName
                    i = i + 1
                Next
 
                Array.Sort(arrFileName)
 
                For i = 0 to Ubound(arrFileName)
 
                    strDocType = Mid(arrFileName(i), 28, 4)
                    strAcctNum = Mid(arrFileName(i), 18, 10)
                    strFileVer = Mid(arrFileName(i), 4, 14)
                    strClinID = LSet(arrFileName(i), 3)
                    If strDocType = "DIMC" Then
                        strDocType = "DIMP"
                    End If
 
                    strDestFile = strDestPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf"
                    strOrgFile = strOrgPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf"
                    
                    System.IO.File.Move(strOrgPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf", strDestPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf")
                    Threading.Thread.Sleep(5000)
 
                Next
 
            End If
 
            Erase arrFileName
        Catch ex As Exception
            errorEmail(ex.ToString)
            System.Environment.Exit(-1)
        End Try
    End Sub
 
End Class

Open in new window

0
glanton
Asked:
glanton
  • 3
1 Solution
 
Bob LearnedCommented:
1) Trouble shooting code in a Windows Service is pretty difficult.

2) My usual approach to this problem is to create a Windows Forms (or Console) application, create the DLLs to do the "heavy lifting", and then add references to the class library in the Windows Service, and call the tested code.  

3) A Windows Service runs in a different environment than a Windows Forms application, so stay away from any UI code.
0
 
glantonAuthor Commented:
Hi TheLearnedOne.

I really appreciate your reply.  I created the DLL and referenced the class library in my service code.  Sadly, it has the same issues.  It tries to move the same file multiple times causing a "could not find file" exception to be thrown."  When I go to debug and press f8 to step through the code appears to be executed randomly.  The highlighted line jumps from place to place.  For example, in the middle of creating the array it will go back to the referenced call in the service code.  Or go back and Dim statements where the variables are declared.

However, when I do the same thing (reference the DLL) through a form application, and click the button the code executes properly.  

Again thank you for your reply!!
0
 
QuintainTCommented:
You are probably getting the random debugging because your timer event is firing for a second time while you are debugging through the first timer event, so the debugger is trying to jump through 2 sessions at the same time. I usually deal with this by setting a flag on at the beginning of the event and off at the end. Then when a timer event fires you check the flag and don't process if it's on.

This might also be the cause of the file not found errors as you may have 2 threads trying to do the same thing.
0
 
glantonAuthor Commented:
Hi QuintainT

I hadn't thought of that.  That sounds like a very good possibility.  I am going to work on trying your idea right now.  I will post back with the result.  

Thank you!!
0
 
glantonAuthor Commented:
HI QuintainT,
You where absolutely correct!! Thank you so much.  It appears I need to dig deeper into the System.Timers namespace :)
Thanks again!!  

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now