Cross-thread operation not valid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    Cross-thread operation not valid

    Hi again,

    Im using vb 2005

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

    [code=vbnet]
    Imports System.IO
    Imports System.Diagnost ics


    Public Class Form1

    Public watchfolder As FileSystemWatch er


    Private Sub btn_startwatch_ Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btn_startwatch. Click
    watchfolder = New System.IO.FileS ystemWatcher()

    'this is the path we want to monitor
    watchfolder.Pat h = txt_watchpath.T ext

    '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.Not ifyFilter = IO.NotifyFilter s.DirectoryName
    watchfolder.Not ifyFilter = watchfolder.Not ifyFilter Or _
    IO.NotifyFilter s.FileName
    watchfolder.Not ifyFilter = watchfolder.Not ifyFilter Or _
    IO.NotifyFilter s.Attributes

    ' add the handler to each event
    AddHandler watchfolder.Cha nged, AddressOf logchange
    AddHandler watchfolder.Cre ated, AddressOf logchange
    AddHandler watchfolder.Del eted, AddressOf logchange

    ' add the rename handler as the signature is different
    AddHandler watchfolder.Ren amed, AddressOf logrename

    'Set this property to true to start watching
    watchfolder.Ena bleRaisingEvent s = True

    btn_startwatch. Enabled = False
    btn_stop.Enable d = True

    'End of code for btn_start_click

    End Sub

    Private Sub logchange(ByVal source As Object, ByVal e As _
    System.IO.FileS ystemEventArgs)
    If e.ChangeType = IO.WatcherChang eTypes.Changed Then
    txt_folderactiv ity.Text &= "File " & e.FullPath & _
    " has been modified" & vbCrLf
    End If
    If e.ChangeType = IO.WatcherChang eTypes.Created Then
    txt_folderactiv ity.Text &= "File " & e.FullPath & _
    " has been created" & vbCrLf
    End If
    If e.ChangeType = IO.WatcherChang eTypes.Deleted Then
    txt_folderactiv ity.Text &= "File " & e.FullPath & _
    " has been deleted" & vbCrLf
    End If
    End Sub
    Public Sub logrename(ByVal source As Object, ByVal e As _
    System.IO.Renam edEventArgs)
    txt_folderactiv ity.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.EventArg s) Handles btn_stop.Click
    ' Stop watching the folder
    watchfolder.Ena bleRaisingEvent s = False
    btn_startwatch. Enabled = True
    btn_stop.Enable d = False

    End Sub
    End Class

    [/code]

    I get this error: on line: 47
    [code=text]
    Cross-thread operation not valid: Control 'txt_folderacti vity' accessed from a thread other than the thread it was created on.
    [/code]

    Can anyone suggest anything, or tell me what this error means?

    James
  • jjvainav
    New Member
    • Feb 2008
    • 25

    #2
    The FileSystemWatch er 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.

    [CODE=vbnet]
    Private Delegate Sub FileWatcherCall back(ByVal e As FileSystemEvent Args)

    Private Sub logchange(ByVal source As Object, ByVal e As FileSystemEvent Args)
    If txt_folderactiv ity.InvokeRequi red Then
    txt_folderactiv ity.Invoke(New FileWatcherCall back(AddressOf HandleFileWatch erEvent), e)
    End If
    End Sub

    Private Sub HandleFileWatch erEvent(ByVal e As FileSystemEvent Args)
    txt_folderactiv ity.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
    End Sub
    [/CODE]

    Hope this helps.
    Last edited by Plater; Feb 28 '08, 02:29 PM. Reason: code=vbnet, makes it colorized

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      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:
      [code=vbnet]
      System.Windows. Forms.Control.C heckForIllegalC rossThreadCalls = false
      [/code]
      It made the problem go away.

      James

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        This is an example page that should help you understand a little bit better.

        Comment

        Working...