473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Thread problem using FileSystemWatcher

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
Mar 13 '07 #1
7 6036
active,

Look up Control.Invoke in the online help.

Kerry Moorman
"active" wrote:
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
Mar 13 '07 #2
" active" <ac**********@a-znet.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 13 '07 #3
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

"Kerry Moorman" <Ke**********@discussions.microsoft.comwrote in message
news:61**********************************@microsof t.com...
active,

Look up Control.Invoke in the online help.

Kerry Moorman
"active" wrote:
>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

Mar 13 '07 #4
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.
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:e3**************@TK2MSFTNGP03.phx.gbl...
>" active" <ac**********@a-znet.comschrieb:
>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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 13 '07 #5
" active" <ac**********@a-znet.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 13 '07 #6
On Mar 13, 11:46 am, " active" <activeNOS...@a-znet.comwrote:
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.

Mar 13 '07 #7
thanks to both of you
" active" <ac**********@a-znet.comwrote in message
news:uq**************@TK2MSFTNGP02.phx.gbl...
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.
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:e3**************@TK2MSFTNGP03.phx.gbl...
>>" active" <ac**********@a-znet.comschrieb:
>>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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Mar 13 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen Miller | last post by:
I have an ASP.Net application that sends a NetworkStream to a .Net Service, which has a TcpListener listening on a port for the ASP.Net client. When it receives a request it creates a new thread...
0
by: Rich Miller | last post by:
Any ideas appreciated. I have written a service application in VB (VS.Net 2003). On start, the service creates an object that instantiates a System.IO.FileSystemWatcher like so (the _watcher is...
9
by: dinny | last post by:
In the filesystemwatcher help page (overview) there is a vb sample which prints on a console windows. I want to change slightly this sample so that instead of using a console window, I use a...
4
by: ljh | last post by:
I am trying to run some pretty simple code that monitors a folder for changes (copied the example at http://www.codeproject.com/dotnet/folderwatcher.asp). But, when I try and update a textbox...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
5
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I...
4
by: Smithers | last post by:
Does the FileSystemWatcher class, when enabled, spawn a new thread for itself? I googled and somehow just found people having random problems with the FileSystemWatcher class and the MSDN docs....
1
by: Nicolas | last post by:
Doesn't run properly. I made one dll which monitor file and send back an event for status on changes. I use the IO.FileSystemWatcher From the window form I want to check 5 txt file in particular...
1
jamesd0142
by: jamesd0142 | last post by:
Hi again, Im using vb 2005 Im using this code to watch a folder and log any changes etc... Imports System.IO Imports System.Diagnostics
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.