Jose Cuervo
Member
- Joined
- Jul 14, 2010
- Messages
- 14
- Programming Experience
- 1-3
Hi,
Right i have managed to get backgroundworking working, but it has gave me an issue when i try to add it to a program i already have lol
I have an application that monitors a folder, and when a file is created it performs some actions. I have made the actions into a background worker and it works great, for a single file. If i try to run it when mutliple files hit the folder, it crashes, as it has to archive/delete the original file, and by then the variable i am using for the file name has changed.
The only way i know how to pass my file names is to create global variable (not a great idea) But i don't know what else i can do? I been stuck at this for ages lol
Here is the code i am using which does the folder monitoring, the triggers the background worker.
In my background worker i have the line "Kill(sFileName)" which is when i am using multiple file, it all goes belly up lol
I still want the UI to be free and not lock up, but just can't figure out what to do.
Please help
Right i have managed to get backgroundworking working, but it has gave me an issue when i try to add it to a program i already have lol
I have an application that monitors a folder, and when a file is created it performs some actions. I have made the actions into a background worker and it works great, for a single file. If i try to run it when mutliple files hit the folder, it crashes, as it has to archive/delete the original file, and by then the variable i am using for the file name has changed.
The only way i know how to pass my file names is to create global variable (not a great idea) But i don't know what else i can do? I been stuck at this for ages lol
Here is the code i am using which does the folder monitoring, the triggers the background worker.
VB.NET:
Dim sFileName As String
Dim watcher As New FileSystemWatcher
Private WithEvents BackgroundWorker As System.ComponentModel.BackgroundWorker
Delegate Sub CallBackToUIThread(ByVal e As FileSystemEventArgs)
'This is the section that watches for files being created in the directory
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
watcher.Path = My.Settings.folderToMonitor
watcher.NotifyFilter = NotifyFilters.FileName
AddHandler watcher.Created, AddressOf onChanged
watcher.EnableRaisingEvents = True
End Sub
Public Sub NotifyUIThreadOfChange(ByVal e As FileSystemEventArgs)
If e.ChangeType = WatcherChangeTypes.Created Then
sFileName = e.FullPath
BackgroundWorker = New System.ComponentModel.BackgroundWorker
BackgroundWorker.WorkerReportsProgress = True
BackgroundWorker.RunWorkerAsync()
End If
End Sub
Private Sub onChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
'Call back to the UI thread
Me.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New CallBackToUIThread(AddressOf NotifyUIThreadOfChange), e)
End Sub
In my background worker i have the line "Kill(sFileName)" which is when i am using multiple file, it all goes belly up lol
I still want the UI to be free and not lock up, but just can't figure out what to do.
Please help