Solved

Windows Service To Parse and Move Files

Posted on 2009-04-08
5
504 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
  • 3
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now