Thread problem using FileSystemWatcher

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • active

    #1

    Thread problem using FileSystemWatcher

    I have:

    Friend Sub WatchFullPath(B yVal zz As FileSystemEvent Handler)

    Dim watcher As New FileSystemWatch er()

    watcher.Path = Disk.GetPath(gF ullPath)

    watcher.NotifyF ilter = NotifyFilters.L astWrite

    watcher.Filter = Disk.GetLastIte m(gFullPath)

    AddHandler watcher.Changed , zz

    watcher.EnableR aisingEvents = True

    and:

    Friend Sub FileChanged(ByV al source As Object, ByVal e As
    FileSystemEvent Args)

    ComboBoxIngredi ents.Items.Clea r()

    End Sub

    and:

    WatchFullPath(A ddressOf FileChanged)



    When I change the file I get, pointing to

    ComboBoxIngredi ents.Items.Clea r():

    the following:

    Cross-thread operation not valid: Control 'ComboBoxIngred ients' accessed
    from a thread other than the thread it was created on.



    Any suggestions on how to fix this?





    Thanks in advance


  • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

    #2
    RE: Thread problem using FileSystemWatch er

    active,

    Look up Control.Invoke in the online help.

    Kerry Moorman


    "active" wrote:
    I have:
    >
    Friend Sub WatchFullPath(B yVal zz As FileSystemEvent Handler)
    >
    Dim watcher As New FileSystemWatch er()
    >
    watcher.Path = Disk.GetPath(gF ullPath)
    >
    watcher.NotifyF ilter = NotifyFilters.L astWrite
    >
    watcher.Filter = Disk.GetLastIte m(gFullPath)
    >
    AddHandler watcher.Changed , zz
    >
    watcher.EnableR aisingEvents = True
    >
    and:
    >
    Friend Sub FileChanged(ByV al source As Object, ByVal e As
    FileSystemEvent Args)
    >
    ComboBoxIngredi ents.Items.Clea r()
    >
    End Sub
    >
    and:
    >
    WatchFullPath(A ddressOf FileChanged)
    >
    >
    >
    When I change the file I get, pointing to
    >
    ComboBoxIngredi ents.Items.Clea r():
    >
    the following:
    >
    Cross-thread operation not valid: Control 'ComboBoxIngred ients' accessed
    from a thread other than the thread it was created on.
    >
    >
    >
    Any suggestions on how to fix this?
    >
    >
    >
    >
    >
    Thanks in advance
    >
    >
    >

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Thread problem using FileSystemWatch er

      " active" <activeNOSPAM @a-znet.comschrieb :
      Friend Sub WatchFullPath(B yVal zz As FileSystemEvent Handler)
      >
      Dim watcher As New FileSystemWatch er()
      >
      watcher.Path = Disk.GetPath(gF ullPath)
      >
      watcher.NotifyF ilter = NotifyFilters.L astWrite
      >
      watcher.Filter = Disk.GetLastIte m(gFullPath)
      >
      AddHandler watcher.Changed , zz
      >
      watcher.EnableR aisingEvents = True
      >
      and:
      >
      Friend Sub FileChanged(ByV al source As Object, ByVal e As
      FileSystemEvent Args)
      >
      ComboBoxIngredi ents.Items.Clea r()
      >
      End Sub
      >
      and:
      >
      WatchFullPath(A ddressOf FileChanged)
      >
      >
      >
      When I change the file I get, pointing to
      >
      ComboBoxIngredi ents.Items.Clea r():
      >
      the following:
      >
      Cross-thread operation not valid: Control 'ComboBoxIngred ients' accessed
      from a thread other than the thread it was created on.
      Set the file system watcher's 'SynchronizingO bject' 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/>

      Comment

      • active

        #4
        Re: Thread problem using FileSystemWatch er

        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
        SynchronizingOb ject
        which I set to the control calling filesystemwatch er
        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" <KerryMoorman@d iscussions.micr osoft.comwrote in message
        news:614E5BA5-D81E-44CB-A8F1-AAEF0BDF3E5B@mi crosoft.com...
        active,
        >
        Look up Control.Invoke in the online help.
        >
        Kerry Moorman
        >
        >
        "active" wrote:
        >
        >I have:
        >>
        >Friend Sub WatchFullPath(B yVal zz As FileSystemEvent Handler)
        >>
        >Dim watcher As New FileSystemWatch er()
        >>
        >watcher.Path = Disk.GetPath(gF ullPath)
        >>
        >watcher.Notify Filter = NotifyFilters.L astWrite
        >>
        >watcher.Filt er = Disk.GetLastIte m(gFullPath)
        >>
        >AddHandler watcher.Changed , zz
        >>
        >watcher.Enable RaisingEvents = True
        >>
        >and:
        >>
        >Friend Sub FileChanged(ByV al source As Object, ByVal e As
        >FileSystemEven tArgs)
        >>
        >ComboBoxIngred ients.Items.Cle ar()
        >>
        >End Sub
        >>
        >and:
        >>
        >WatchFullPath( AddressOf FileChanged)
        >>
        >>
        >>
        >When I change the file I get, pointing to
        >>
        >ComboBoxIngred ients.Items.Cle ar():
        >>
        >the following:
        >>
        >Cross-thread operation not valid: Control 'ComboBoxIngred ients' accessed
        >from a thread other than the thread it was created on.
        >>
        >>
        >>
        >Any suggestions on how to fix this?
        >>
        >>
        >>
        >>
        >>
        >Thanks in advance
        >>
        >>
        >>

        Comment

        • active

          #5
          Re: Thread problem using FileSystemWatch er

          While reading about Control.Invoke I stumbled across SynchronizingOb ject 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]" <hirf-spam-me-here@gmx.atwrot e in message
          news:e3ZFgMYZHH A.1220@TK2MSFTN GP03.phx.gbl...
          >" active" <activeNOSPAM @a-znet.comschrieb :
          >Friend Sub WatchFullPath(B yVal zz As FileSystemEvent Handler)
          >>
          >Dim watcher As New FileSystemWatch er()
          >>
          >watcher.Path = Disk.GetPath(gF ullPath)
          >>
          >watcher.Notify Filter = NotifyFilters.L astWrite
          >>
          >watcher.Filt er = Disk.GetLastIte m(gFullPath)
          >>
          >AddHandler watcher.Changed , zz
          >>
          >watcher.Enable RaisingEvents = True
          >>
          >and:
          >>
          >Friend Sub FileChanged(ByV al source As Object, ByVal e As
          >FileSystemEven tArgs)
          >>
          >ComboBoxIngred ients.Items.Cle ar()
          >>
          >End Sub
          >>
          >and:
          >>
          >WatchFullPath( AddressOf FileChanged)
          >>
          >>
          >>
          >When I change the file I get, pointing to
          >>
          >ComboBoxIngred ients.Items.Cle ar():
          >>
          >the following:
          >>
          >Cross-thread operation not valid: Control 'ComboBoxIngred ients' accessed
          >from a thread other than the thread it was created on.
          >
          Set the file system watcher's 'SynchronizingO bject' 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/>

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Thread problem using FileSystemWatch er

            " active" <activeNOSPAM @a-znet.comschrieb :
            While reading about Control.Invoke I stumbled across SynchronizingOb ject
            and tried it but wasn't sure it what I did was OK.
            If 'SynchronizingO bject' 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/>

            Comment

            • Brian Gideon

              #7
              Re: Thread problem using FileSystemWatch er

              On Mar 13, 11:46 am, " active" <activeNOS... @a-znet.comwrote:
              While reading about Control.Invoke I stumbled across SynchronizingOb ject 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 SynchronizingOb ject is set the
              FileSystemWatch er calls Invoke automatically to marshal the execution
              of the event handlers onto the thread hosting the
              SynchronizingOb ject. If that object is a Form or Control then we're
              talking about the UI thread.

              Comment

              • active

                #8
                Re: Thread problem using FileSystemWatch er

                thanks to both of you
                " active" <activeNOSPAM @a-znet.comwrote in message
                news:uqPz6aYZHH A.3628@TK2MSFTN GP02.phx.gbl...
                While reading about Control.Invoke I stumbled across SynchronizingOb ject
                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]" <hirf-spam-me-here@gmx.atwrot e in message
                news:e3ZFgMYZHH A.1220@TK2MSFTN GP03.phx.gbl...
                >>" active" <activeNOSPAM @a-znet.comschrieb :
                >>Friend Sub WatchFullPath(B yVal zz As FileSystemEvent Handler)
                >>>
                >>Dim watcher As New FileSystemWatch er()
                >>>
                >>watcher.Pat h = Disk.GetPath(gF ullPath)
                >>>
                >>watcher.Notif yFilter = NotifyFilters.L astWrite
                >>>
                >>watcher.Filte r = Disk.GetLastIte m(gFullPath)
                >>>
                >>AddHandler watcher.Changed , zz
                >>>
                >>watcher.Enabl eRaisingEvents = True
                >>>
                >>and:
                >>>
                >>Friend Sub FileChanged(ByV al source As Object, ByVal e As
                >>FileSystemEve ntArgs)
                >>>
                >>ComboBoxIngre dients.Items.Cl ear()
                >>>
                >>End Sub
                >>>
                >>and:
                >>>
                >>WatchFullPath (AddressOf FileChanged)
                >>>
                >>>
                >>>
                >>When I change the file I get, pointing to
                >>>
                >>ComboBoxIngre dients.Items.Cl ear():
                >>>
                >>the following:
                >>>
                >>Cross-thread operation not valid: Control 'ComboBoxIngred ients' accessed
                >>from a thread other than the thread it was created on.
                >>
                >Set the file system watcher's 'SynchronizingO bject' 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/>
                >
                >

                Comment

                Working...