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
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    [code=vbnet]
    System.Windows. Forms.Control.C heckForIllegalC rossThreadCalls = false
    [/code]

    Made the problem go away.

    James

    Comment

    Working...