473,396 Members | 1,891 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,396 software developers and data experts.

Cross-thread operation not valid

jamesd0142
469 256MB
Hi again,

Im using vb 2005

Im using this code to watch a folder and log any changes etc...

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.Diagnostics
  3.  
  4.  
  5. Public Class Form1
  6.  
  7.     Public watchfolder As FileSystemWatcher
  8.  
  9.  
  10.     Private Sub btn_startwatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_startwatch.Click
  11.         watchfolder = New System.IO.FileSystemWatcher()
  12.  
  13.         'this is the path we want to monitor
  14.         watchfolder.Path = txt_watchpath.Text
  15.  
  16.         'Add a list of Filter we want to specify
  17.         'make sure you use OR for each Filter as we need to
  18.         'all of those
  19.  
  20.         watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
  21.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
  22.                                    IO.NotifyFilters.FileName
  23.         watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
  24.                                    IO.NotifyFilters.Attributes
  25.  
  26.         ' add the handler to each event
  27.         AddHandler watchfolder.Changed, AddressOf logchange
  28.         AddHandler watchfolder.Created, AddressOf logchange
  29.         AddHandler watchfolder.Deleted, AddressOf logchange
  30.  
  31.         ' add the rename handler as the signature is different
  32.         AddHandler watchfolder.Renamed, AddressOf logrename
  33.  
  34.         'Set this property to true to start watching
  35.         watchfolder.EnableRaisingEvents = True
  36.  
  37.         btn_startwatch.Enabled = False
  38.         btn_stop.Enabled = True
  39.  
  40.         'End of code for btn_start_click
  41.  
  42.     End Sub
  43.  
  44.     Private Sub logchange(ByVal source As Object, ByVal e As _
  45.                       System.IO.FileSystemEventArgs)
  46.         If e.ChangeType = IO.WatcherChangeTypes.Changed Then
  47.             txt_folderactivity.Text &= "File " & e.FullPath & _
  48.                                     " has been modified" & vbCrLf
  49.         End If
  50.         If e.ChangeType = IO.WatcherChangeTypes.Created Then
  51.             txt_folderactivity.Text &= "File " & e.FullPath & _
  52.                                     " has been created" & vbCrLf
  53.         End If
  54.         If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
  55.             txt_folderactivity.Text &= "File " & e.FullPath & _
  56.                                     " has been deleted" & vbCrLf
  57.         End If
  58.     End Sub
  59.     Public Sub logrename(ByVal source As Object, ByVal e As _
  60.                          System.IO.RenamedEventArgs)
  61.         txt_folderactivity.Text &= "File" & e.OldName & _
  62.                       " has been renamed to " & e.Name & vbCrLf
  63.     End Sub
  64.  
  65.     Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
  66.         ' Stop watching the folder
  67.         watchfolder.EnableRaisingEvents = False
  68.         btn_startwatch.Enabled = True
  69.         btn_stop.Enabled = False
  70.  
  71.     End Sub
  72. End Class
  73.  
  74.  
I get this error: on line: 47
Expand|Select|Wrap|Line Numbers
  1. Cross-thread operation not valid: Control 'txt_folderactivity' accessed from a thread other than the thread it was created on.
  2.  
Can anyone suggest anything, or tell me what this error means?

James
Feb 28 '08 #1
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.

Expand|Select|Wrap|Line Numbers
  1. Private Delegate Sub FileWatcherCallback(ByVal e As FileSystemEventArgs)
  2.  
  3. Private Sub logchange(ByVal source As Object, ByVal e As  FileSystemEventArgs)
  4.         If txt_folderactivity.InvokeRequired Then
  5.             txt_folderactivity.Invoke(New FileWatcherCallback(AddressOf HandleFileWatcherEvent), e)
  6.         End If
  7. End Sub
  8.  
  9. Private Sub HandleFileWatcherEvent(ByVal e As FileSystemEventArgs)
  10.         txt_folderactivity.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
  11. End Sub
  12.  
Hope this helps.
Feb 28 '08 #2
jamesd0142
469 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false
  2.  
It made the problem go away.

James
Feb 28 '08 #3
Plater
7,872 Expert 4TB
This is an example page that should help you understand a little bit better.
http://msdn2.microsoft.com/en-us/library/ms171728.aspx
Feb 28 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
3
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...
0
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,...
4
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...
0
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,...
1
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...
6
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...
6
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...
6
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.
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: 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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.