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
-
-
-
Public Class Form1
-
-
Public watchfolder As FileSystemWatcher
-
-
-
Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click
-
watchfolder = New System.IO.FileSystemWatcher()
-
-
'this is the path we want to monitor
-
watchfolder.Path = txt_watchpath.Text
-
-
'Add a list of Filter we want to specify
-
'make sure you use OR for each Filter as we need to
-
'all of those
-
-
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
-
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
-
IO.NotifyFilters.FileName
-
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
-
IO.NotifyFilters.Attributes
-
-
' add the handler to each event
-
AddHandler watchfolder.Changed, AddressOf logchange
-
AddHandler watchfolder.Created, AddressOf logchange
-
AddHandler watchfolder.Deleted, AddressOf logchange
-
-
' add the rename handler as the signature is different
-
AddHandler watchfolder.Renamed, AddressOf logrename
-
-
'Set this property to true to start watching
-
watchfolder.EnableRaisingEvents = True
-
-
btn_startwatch.Enabled = False
-
btn_stop.Enabled = True
-
-
'End of code for btn_start_click
-
-
End Sub
-
-
Private Sub logchange(ByVal source As Object, ByVal e As _
-
System.IO.FileSystemEventArgs)
-
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
-
txt_folderactivity.Text &= "File " & e.FullPath & _
-
" has been modified" & vbCrLf
-
End If
-
If e.ChangeType = IO.WatcherChangeTypes.Created Then
-
txt_folderactivity.Text &= "File " & e.FullPath & _
-
" has been created" & vbCrLf
-
End If
-
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
-
txt_folderactivity.Text &= "File " & e.FullPath & _
-
" has been deleted" & vbCrLf
-
End If
-
End Sub
-
Public Sub logrename(ByVal source As Object, ByVal e As _
-
System.IO.RenamedEventArgs)
-
txt_folderactivity.Text &= "File" & e.OldName & _
-
" has been renamed to " & e.Name & vbCrLf
-
End Sub
-
-
Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
-
' Stop watching the folder
-
watchfolder.EnableRaisingEvents = False
-
btn_startwatch.Enabled = True
-
btn_stop.Enabled = False
-
-
End Sub
-
End Class
-
-
I get this error: on line: 47 -
Cross-thread operation not valid: Control 'txt_folderactivity' accessed from a thread other than the thread it was created on.
-
Can anyone suggest anything, or tell me what this error means?
James
3 1439
The FileSystemWatcher runs on a seperate thread, when you try to set the value of your TextBox .NET doesn't allow you to access the control from another thread, and will throw the exception you just specified.
What you need to do is call back to the main thread that your UI is running on. Here is an example on how you can accomplish this. -
Private Delegate Sub FileWatcherCallback(ByVal e As FileSystemEventArgs)
-
-
Private Sub logchange(ByVal source As Object, ByVal e As FileSystemEventArgs)
-
If txt_folderactivity.InvokeRequired Then
-
txt_folderactivity.Invoke(New FileWatcherCallback(AddressOf HandleFileWatcherEvent), e)
-
End If
-
End Sub
-
-
Private Sub HandleFileWatcherEvent(ByVal e As FileSystemEventArgs)
-
txt_folderactivity.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
-
End Sub
-
Hope this helps.
Thanks for the reply!
I'm affraid i don't understand, it's a little to complicated for me... Dont think I've done anything like this before.
so instead i did this: -
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false
-
It made the problem go away.
James
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Web Science |
last post by:
Site and Features: http://www.eigensearch.com
Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
|
by: * ProteanThread * |
last post by:
but depends upon the clique:
...
|
by: rollasoc |
last post by:
Hi,
Doing a bit of system testing on a Windows 98 laptop. (.Net 1.1 app).
Did a bit of testing. Loaded a previously saved file. A gray box
appeared with the text and buttons all white...
|
by: Web Science |
last post by:
Site and Features: http://www.eigensearch.com
Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
|
by: David Peach |
last post by:
Hello, hope somebody here can help me... I have a query that lists
defects recorded in a user defined date range. That query is then used
as the source for a Cross Tab query that cross-tabs count...
|
by: Web Science |
last post by:
Site and Features: http://www.eigensearch.com
Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
|
by: Rob Woodworth |
last post by:
Hi,
I'm having serious problems getting my report to work. I need to
generate a timesheet report which will contain info for one employee between
certain dates (one week's worth of dates). I...
|
by: Simon |
last post by:
Hi All,
An experiment i'm doing requires requires a synchronous cross-domain
request, without using a proxy. I wondered if anyone had any ideas to
help me achieve this.
Below is what I have...
|
by: Bart Van der Donck |
last post by:
Hello,
I'm presenting my new library 'AJAX Cross Domain' - a javascript
extension that allows to perform cross-domain AJAX requests.
http://www.ajax-cross-domain.com/
Any comments or...
|
by: ampo |
last post by:
Hello.
Can anyone help with cross-domain problem?
I have HTML page from server1 that send xmlHTTPRequest to server2.
How can I do it?
Thanks.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |