Solved

Windows Service To Parse and Move Files

Posted on 2009-04-08
5
522 Views
Last Modified: 2012-05-06
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
Comment
Question by:glanton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24107110
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
 

Author Comment

by:glanton
ID: 24108120
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
 
LVL 5

Accepted Solution

by:
QuintainT earned 250 total points
ID: 24110108
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
 

Author Comment

by:glanton
ID: 24110566
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
 

Author Closing Comment

by:glanton
ID: 31568297
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question