Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows form and filesystem watcher threading issue

Windows form and filesystem watcher threading issue

Scheduled Pinned Locked Moved C#
helpquestionannouncement
8 Posts 4 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tsiv
    wrote on last edited by
    #1

    Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas

    V O 3 Replies Last reply
    0
    • T tsiv

      Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #2

      Must say I'm chuffed - I figured out the problem by reading just the subject. :-O I had the same issue in a different context the other day; the problem is that Winforms controls are not thread safe. I'm not at work right now, so I don't have the code, but you will have to use a delegate in your form class to point to the method that actually does the work. In your event handler, you will have to invoke the delegate like

      this.Invoke(new YourDelegate(TheMethodThatUpdatesTheControls));

      The copy of MSDN on my colleague's machine was very helpful with this. If you're still having problems, just yell and I will give you the code when I get back to work tomorrow.

      Cheers, विक्रम


      And sleep will come, it comes to us all And some will fade and some will fall

      T 1 Reply Last reply
      0
      • T tsiv

        Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas

        V Offline
        V Offline
        Vikram A Punathambekar
        wrote on last edited by
        #3

        Check out the 'usual solution' described in this article: http://www.codeproject.com/useritems/ISinchronizedInvoke.asp[^]

        Cheers, विक्रम


        And sleep will come, it comes to us all And some will fade and some will fall

        1 Reply Last reply
        0
        • T tsiv

          Hi I have a problem while using the Filesystem watcher component in conjunction with a Windows form application. I'm trying to do the following: The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs... So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes... So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...! When trying to update the panel I get an error saying that: Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on. The line of code that causes this error is: pnlUpdates.Controls.Add(lblTest); I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)... Anyone who knows how this can be done? Kind regards Thomas

          O Offline
          O Offline
          Obaid ur Rehman
          wrote on last edited by
          #4

          You might want to read this article. Hopefully it will clear up a lot of things: http://www.codeproject.com/csharp/begininvoke.asp

          1 Reply Last reply
          0
          • V Vikram A Punathambekar

            Must say I'm chuffed - I figured out the problem by reading just the subject. :-O I had the same issue in a different context the other day; the problem is that Winforms controls are not thread safe. I'm not at work right now, so I don't have the code, but you will have to use a delegate in your form class to point to the method that actually does the work. In your event handler, you will have to invoke the delegate like

            this.Invoke(new YourDelegate(TheMethodThatUpdatesTheControls));

            The copy of MSDN on my colleague's machine was very helpful with this. If you're still having problems, just yell and I will give you the code when I get back to work tomorrow.

            Cheers, विक्रम


            And sleep will come, it comes to us all And some will fade and some will fall

            T Offline
            T Offline
            tsiv
            wrote on last edited by
            #5

            Hi Thanks a lot for the help. Used the invoke method and it works like a charm! Kind regards Thomas

            L 1 Reply Last reply
            0
            • T tsiv

              Hi Thanks a lot for the help. Used the invoke method and it works like a charm! Kind regards Thomas

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              tsiv wrote:

              Used the invoke method and it works like a charm!

              It's not a charm. The behavior and solution are well documented. If you want to be a software developer rather than a snake charmer I strongly suggest you start reading the documentation.

              T 1 Reply Last reply
              0
              • L led mike

                tsiv wrote:

                Used the invoke method and it works like a charm!

                It's not a charm. The behavior and solution are well documented. If you want to be a software developer rather than a snake charmer I strongly suggest you start reading the documentation.

                T Offline
                T Offline
                tsiv
                wrote on last edited by
                #7

                Don't you have something better to do than drop stupid comments on this forum!? Fool. I had a problem - Vikram was kind enough to point me in the direction of a solution - why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that... Thomas

                L 1 Reply Last reply
                0
                • T tsiv

                  Don't you have something better to do than drop stupid comments on this forum!? Fool. I had a problem - Vikram was kind enough to point me in the direction of a solution - why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that... Thomas

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  tsiv wrote:

                  why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that...

                  Of course you don't get it because you don't comprehend the value of reading the documentation. I can point that out to people like you but I can't make you understand it. There is an old proverb, once again I can recite it but I can't make you understand it:          “Give a man a fish and you feed him for a day, teach a man to fish, and you feed him for a lifetime.”

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups