• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

Wait until file has completed transfer before command runs on hotfolder Version 2

In need help to modify the code in solution ID26290103 to monitor multiple folders (not sub folders). Please help, thanks

Solution ID 26290103 code was like this

Imports System.IO
Public Class Form1

    Public Class Files
        Public Shared ToBeProcessed As New Queue(Of String)
        Public Shared SharedObject As New Object
        Public Shared MRE As New System.Threading.ManualResetEvent(False)
    End Class

    Private m_WatchDirectory As String
    Private WithEvents m_FileSystemWatcher As FileSystemWatcher
    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bgw.WorkerReportsProgress = True
        bgw.RunWorkerAsync()

        m_WatchDirectory &= "c:\docume~1\user\desktop\howtofolderwatcher\baton\bad"

        ' Make the FileSystemWatcher.
        m_FileSystemWatcher = New FileSystemWatcher(m_WatchDirectory, "*.mpg")
        m_FileSystemWatcher.NotifyFilter = 0
        m_FileSystemWatcher.NotifyFilter = m_FileSystemWatcher.NotifyFilter Or NotifyFilters.FileName
        m_FileSystemWatcher.EnableRaisingEvents = True
    End Sub

    ' Process a new file.
    Private Sub m_FileSystemWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles m_FileSystemWatcher.Created
        SyncLock Files.SharedObject
            If Not Files.ToBeProcessed.Contains(e.Name) Then
                Files.ToBeProcessed.Enqueue(e.Name)
                Files.MRE.Set()
            End If
        End SyncLock
    End Sub

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim FileName As String
        Dim HasMoreItems As Boolean
        While True
            Files.MRE.Reset()
            HasMoreItems = False
            Do
                Filename = Nothing
                SyncLock Files.SharedObject
                    If Files.ToBeProcessed.Count > 0 Then
                        FileName = Files.ToBeProcessed.Peek
                    End If
                End SyncLock

                If Not IsNothing(FileName) Then
                    Dim P As New Process
                    P.StartInfo.FileName = "cmd"
                    P.StartInfo.Arguments = "/c " & "c:\docume~1\user\desktop\howtofolderwatcher\baton\bad\dtrmxutil.exe " & "c:\docume~1\user\desktop\howtofolderwatcher\baton\bad\" & FileName & " c:\docume~1\marc.viste\desktop\howtofolderwatcher\baton\bad\nulls\" & FileName & " 6000000"
                    Try
                        P.Start()
                        P.WaitForExit()
                        SyncLock Files.SharedObject
                            Files.ToBeProcessed.Dequeue()
                        End SyncLock
                        bgw.ReportProgress(0, FileName)
                    Catch ex As Exception
                        System.Threading.Thread.Sleep(5000) ' wait 5 seconds before trying again
                    End Try
                End If

                SyncLock Files.SharedObject
                    HasMoreItems = (Files.ToBeProcessed.Count > 0)
                End SyncLock
            Loop While HasMoreItems
            Files.MRE.WaitOne()
        End While
    End Sub

    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        lstFiles.Items.Add(Now.ToString() & " Processed " & e.UserState)
    End Sub

End Class

Open in new window

0
Jagan700
Asked:
Jagan700
  • 2
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, try this:
Imports System.IO
Public Class Form1

    Public Class Files
        Public Shared ToBeProcessed As New Queue(Of String)
        Public Shared SharedObject As New Object
        Public Shared MRE As New System.Threading.ManualResetEvent(False)
    End Class

    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bgw.WorkerReportsProgress = True
        bgw.RunWorkerAsync()

        'Folders to watch
        WacthDirectory("C:\Temp\temp2\", "*.mpg")
        WacthDirectory("C:\Temp\Temp1\", "*.mpg")
        WacthDirectory("C:\Temp\FolderB\", "*.mpg")
    End Sub

    Sub WacthDirectory(ByVal path As String, ByVal filter As String)
        Dim fsw As New FileSystemWatcher(path, filter)
        fsw.NotifyFilter = 0
        fsw.NotifyFilter = fsw.NotifyFilter Or NotifyFilters.FileName
        fsw.IncludeSubdirectories = False
        AddHandler fsw.Created, AddressOf m_FileSystemWatcher_Created
        fsw.EnableRaisingEvents = True
    End Sub

    ' Process a new file.
    Private Sub m_FileSystemWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
        SyncLock Files.SharedObject
            If Not Files.ToBeProcessed.Contains(e.Name) Then
                Files.ToBeProcessed.Enqueue(e.Name)
                Files.MRE.Set()
            End If
        End SyncLock
    End Sub

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim FileName As String
        Dim HasMoreItems As Boolean
        While True
            Files.MRE.Reset()
            HasMoreItems = False
            Do
                Filename = Nothing
                SyncLock Files.SharedObject
                    If Files.ToBeProcessed.Count > 0 Then
                        FileName = Files.ToBeProcessed.Peek
                    End If
                End SyncLock

                If Not IsNothing(FileName) Then
                    Dim P As New Process
                    P.StartInfo.FileName = "cmd"
                    P.StartInfo.Arguments = "/c " & "c:\docume~1\user\desktop\howtofolderwatcher\baton\bad\dtrmxutil.exe " & "c:\docume~1\user\desktop\howtofolderwatcher\baton\bad\" & FileName & " c:\docume~1\marc.viste\desktop\howtofolderwatcher\baton\bad\nulls\" & FileName & " 6000000"
                    Try
                        P.Start()
                        P.WaitForExit()
                        SyncLock Files.SharedObject
                            Files.ToBeProcessed.Dequeue()
                        End SyncLock
                        bgw.ReportProgress(0, FileName)
                    Catch ex As Exception
                        System.Threading.Thread.Sleep(5000) ' wait 5 seconds before trying again
                    End Try
                End If

                SyncLock Files.SharedObject
                    HasMoreItems = (Files.ToBeProcessed.Count > 0)
                End SyncLock
            Loop While HasMoreItems
            Files.MRE.WaitOne()
        End While
    End Sub

    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        lstFiles.Items.Add(Now.ToString() & " Processed " & e.UserState)
    End Sub

End Class

Open in new window


I created the WacthDirectory method, you can use it to watch more folders, this not monitor subfolders as you requested.

I hope this help.
0
 
Jagan700Author Commented:
Thaks it works great
I just modified the cmd window to work in background by adding the following lines to the code
 P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Good yo know buddy

0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

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