Thread problem using FileSystemWatcher

  • Thread starter Thread starter Guest
  • Start date Start date
I have:

Friend Sub WatchFullPath(ByVal zz As FileSystemEventHandler)

Dim watcher As New FileSystemWatcher()

watcher.Path = Disk.GetPath(gFullPath)

watcher.NotifyFilter = NotifyFilters.LastWrite

watcher.Filter = Disk.GetLastItem(gFullPath)

AddHandler watcher.Changed, zz

watcher.EnableRaisingEvents = True

and:

Friend Sub FileChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)

ComboBoxIngredients.Items.Clear()

End Sub

and:

WatchFullPath(AddressOf FileChanged)



When I change the file I get, pointing to

ComboBoxIngredients.Items.Clear():

the following:

Cross-thread operation not valid: Control 'ComboBoxIngredients' accessed
from a thread other than the thread it was created on.



Any suggestions on how to fix this?





Thanks in advance
 
active said:
Friend Sub WatchFullPath(ByVal zz As FileSystemEventHandler)

Dim watcher As New FileSystemWatcher()

watcher.Path = Disk.GetPath(gFullPath)

watcher.NotifyFilter = NotifyFilters.LastWrite

watcher.Filter = Disk.GetLastItem(gFullPath)

AddHandler watcher.Changed, zz

watcher.EnableRaisingEvents = True

and:

Friend Sub FileChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)

ComboBoxIngredients.Items.Clear()

End Sub

and:

WatchFullPath(AddressOf FileChanged)



When I change the file I get, pointing to

ComboBoxIngredients.Items.Clear():

the following:

Cross-thread operation not valid: Control 'ComboBoxIngredients' accessed
from a thread other than the thread it was created on.

Set the file system watcher's 'SynchronizingObject' property to the
form/control.
 
active said:
While reading about Control.Invoke I stumbled across SynchronizingObject
and tried it but wasn't sure it what I did was OK.

If 'SynchronizingObject' is set appropriately, then 'Control.Invoke' is not
required.
 
While reading about Control.Invoke I stumbled across SynchronizingObject and
tried it but wasn't sure it what I did was OK.

Thanks for verifying for me that it is a good way to go.

Basically what's happening is that when SynchronizingObject is set the
FileSystemWatcher calls Invoke automatically to marshal the execution
of the event handlers onto the thread hosting the
SynchronizingObject. If that object is a Form or Control then we're
talking about the UI thread.
 
I could figure out how to apply Control.Invoke.
And couldn't find an example.
Do you know of one?
I'd still like to know how to use it since you suggested it.

But while looking I stumbled across
SynchronizingObject
which I set to the control calling filesystemwatcher
and containing the offending controllbox.
I really didn't understand the doc but tried it anyway.
It appears to have solved the problem (so far anyway)
Do you think this is OK to use?

thanks
 
While reading about Control.Invoke I stumbled across SynchronizingObject and
tried it but wasn't sure it what I did was OK.

Thanks for verifying for me that it is a good way to go.
 
thanks to both of you
active said:
While reading about Control.Invoke I stumbled across SynchronizingObject
and tried it but wasn't sure it what I did was OK.

Thanks for verifying for me that it is a good way to go.
 
Back
Top