|
-
Dec 30th, 2003, 09:46 PM
#1
Thread Starter
Junior Member
Wait for file transfer/copy to folder completed - using FileSystemWatcher
Hi,
I'm trying to get notified by application when the folder is added with new file.
The problem is when the file being added to folder is very big....94 MB. And it takes > 15 minutes to complete the file transfer/copy. And I want only be notified by the application when the file transfer is completed.
Currently I'm just able to get notified whenever the file is added to the folder, even if the file transfer/copy process still not completed 
Is there any tips to overcome this problem?
Thank you very much for the help and tips.
regards.
Last edited by bahruddina; Dec 3rd, 2004 at 01:40 AM.
-
Dec 31st, 2003, 09:57 AM
#2
I wonder how many charact
An old hack trick, if the file is still being written to... you could not possibly rename the file, it will error saying another process is using it.
So when you do not get an error, you will know the file transfer is complete.
-
Dec 31st, 2003, 10:18 AM
#3
Thats what I do. i kick off a thread when the Systemwatcher kicks in. It is a loop that tries to open the file exclusivly. if it errors, I sleep the thread for 10 seconds, then try again. Wash rinse repeat. Here is my code which has some other stuff you wont need, but should give you an idea
Code:
Imports System.IO
Imports System.Threading
Module BRS1
Private fileName As String
Private myThread As Thread
Private evLog As EventLog
Private dir As String = System.Configuration.ConfigurationSettings.AppSettings("Watchdir").ToString()
Private MonthEnd As Boolean ' Determine if we are waiting for motnh end errors to be corrected.
Sub Main()
' Create a new FilesystemWatcher object
Console.WriteLine("Type x <Enter> to quit")
Dim fsWatch As New FileSystemWatcher(dir)
fsWatch.IncludeSubdirectories = False
fsWatch.EnableRaisingEvents = True
AddHandler fsWatch.Created, AddressOf File_Created
Do Until Console.ReadLine() = "x"
Loop
End Sub
Private Sub File_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
' when the file is created, this event will fire. start a new thread to attempt to open the file.
fileName = e.FullPath
myThread = New Thread(AddressOf OpenFileThread)
myThread.Start()
End Sub
Private Sub OpenFileThread()
Dim openFile As Stream
Dim fileOpened As Boolean
Dim boolFileMoved As Boolean
Dim objFileOps As New BRSFileOperations()
Dim newFileName As String
Dim boolValidated As Boolean
Dim fileGUID As String = Guid.NewGuid.ToString() ' Create a GUID for use in file nameing.
Dim METhread As Thread
Do Until fileOpened
Try
' Attempt to open the file.
openFile = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
' If code gets here, move and rename file before processing.
If Not (boolFileMoved) Then
openFile.Close()
' Create new filename
newFileName = dir & "\process\" & fileGUID
File.Move(fileName, newFileName)
boolFileMoved = True
fileName = newFileName
' Attempt to open the file again
openFile = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
' With the boolfilemoved set to true, if it errors above, it should skip this next time
End If
Console.WriteLine("Process started at " & System.DateTime.Now())
' Validate if the file is a good file
boolValidated = objFileOps.ValidateFile(openFile)
' TODO: Begin Month End wait
If MonthEnd = True Then
METhread = New Thread(AddressOf MonthEndThread)
METhread.Start()
Do Until MonthEnd = False
Loop
End If
' begin parsing if it is valid
If boolValidated Then
objFileOps.ParseFile(openFile)
End If
openFile.Close()
objFileOps = Nothing
fileOpened = True
Catch dirnotfound As DirectoryNotFoundException
' This should only be thrown during the Move command
evLog.WriteEntry("Application", dirnotfound.ToString(), EventLogEntryType.Error)
Thread.CurrentThread.Abort()
Catch filenotfound As FileNotFoundException
' Soemthing happened to the file before we opened it or the process started prematurly.
' End this process
Thread.CurrentThread.Abort()
Catch fileinuse As IOException
' Generic IO Error. No specifc check for file in use.
Console.WriteLine("Could not open file: " & fileName)
' Sleep thread for 10 seconds so it can try again later.
Thread.CurrentThread.Sleep(10000)
Catch ex As Exception
evLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error)
Console.WriteLine(ex.ToString())
' Some unknown error. May need to shut the Thread down completey
Thread.CurrentThread.Abort()
Finally
If fileOpened Then
' if everything went ok, then end the thread
Console.WriteLine("Process finished at " & System.DateTime.Now())
' TODO: Archive file to zip file for storage
End If
End Try
Loop
Thread.CurrentThread.Abort()
End Sub
Private Sub MonthEndThread()
' We need to check every so often if all errors in db has been corrected
' Do a cehck.
' If errors still exist, sleep for 30 minutes
' if 0 found, set motnhend boolen = false and abort thread
Dim sqlComm As SqlClient.SqlCommand
Dim conn As System.Data.SqlClient.SqlConnection
Dim daMonthEnd As SqlClient.SqlDataAdapter
Dim connstring As String = System.Configuration.ConfigurationSettings.AppSettings("SQLConn").ToString()
sqlComm = New SqlClient.SqlCommand("Select count(*) from blah")
Do
Thread.CurrentThread.Sleep(600000)
Loop
Thread.CurrentThread.Abort()
MonthEnd = False
End Sub
End Module
-
Jan 1st, 2004, 09:40 PM
#4
Thread Starter
Junior Member
thanx a lot. It works like a champ
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|