Backgroundworker and controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny Jörgensen

    Backgroundworker and controls

    Hi I have a procudure that takes some time and thus slows down the main
    system. I want to put it in a backgroundworke r component to run it
    asynchroneously . But in the procedure, I want to update a TreeView control I
    have on my main form.

    When I try to do that, I get the error "Cross-thread operation not valid:
    Control 'TreeView1' accessed from a thread other than the thread it was
    created on"

    I seem to have read something about that poblem before, but can't remember
    if there's a solution to it...

    Please advice asap,
    Johnny J.


  • Armin Zingler

    #2
    Re: Backgroundworke r and controls

    "Johnny Jörgensen" <jojo@altcom.se schrieb
    Hi I have a procudure that takes some time and thus slows down the
    main system. I want to put it in a backgroundworke r component to run
    it asynchroneously . But in the procedure, I want to update a
    TreeView control I have on my main form.
    >
    When I try to do that, I get the error "Cross-thread operation not
    valid: Control 'TreeView1' accessed from a thread other than the
    thread it was created on"
    >
    I seem to have read something about that poblem before, but can't
    remember if there's a solution to it...
    >
    Please advice asap,
    Johnny J.

    Call the control's BeginInvoke function ([F1] for details). The invoked
    function will run in the thread that created the control, which is allowed
    to access it.


    Armin

    Comment

    • Johnny Jörgensen

      #3
      Re: Backgroundworke r and controls

      But won't that be the same thread as my main thread???

      /Johnny J.



      "Armin Zingler" <az.nospam@free net.deskrev i meddelandet
      news:uc3uE4C1HH A.1484@TK2MSFTN GP06.phx.gbl...
      "Johnny Jörgensen" <jojo@altcom.se schrieb
      >Hi I have a procudure that takes some time and thus slows down the
      >main system. I want to put it in a backgroundworke r component to run
      >it asynchroneously . But in the procedure, I want to update a
      >TreeView control I have on my main form.
      >>
      >When I try to do that, I get the error "Cross-thread operation not
      >valid: Control 'TreeView1' accessed from a thread other than the
      >thread it was created on"
      >>
      >I seem to have read something about that poblem before, but can't
      >remember if there's a solution to it...
      >>
      >Please advice asap,
      >Johnny J.
      >
      >
      Call the control's BeginInvoke function ([F1] for details). The invoked
      function will run in the thread that created the control, which is allowed
      to access it.
      >
      >
      Armin
      >

      Comment

      • rowe_newsgroups

        #4
        Re: Backgroundworke r and controls

        But won't that be the same thread as my main thread???

        Yes.

        The problem is that the background worker's thread is not your main
        thread, which is why you need to use BeginInvoke to delegate control
        back to the main thread.

        Thanks,

        Seth Rowe

        Comment

        • Phill W.

          #5
          Re: Backgroundworke r and controls

          Johnny Jörgensen wrote:
          Hi I have a procudure that takes some time and thus slows down the main
          system. I want to put it in a backgroundworke r component to run it
          asynchroneously . But in the procedure, I want to update a TreeView control I
          have on my main form.
          >
          When I try to do that, I get the error "Cross-thread operation not valid:
          Control 'TreeView1' accessed from a thread other than the thread it was
          created on"
          But the whole point of the Background worker is that it raises events
          (ProgressChange d, RunWorkerComple ted, etc) as the background job proceeds.

          Most importantly, these events are raised /on the UI thread/, so you can
          update Controls any way you like.

          HTH,
          Phill W.

          Comment

          • =?iso-8859-1?q?Horacio_Nu=F1ez_Hern=E1ndez?=

            #6
            Re: Backgroundworke r and controls

            On 1 ago, 07:01, "Johnny Jörgensen" <j...@altcom.se wrote:
            Hi I have a procudure that takes some time and thus slows down the main
            system. I want to put it in a backgroundworke r component to run it
            asynchroneously . But in the procedure, I want to update a TreeView control I
            have on my main form.
            >
            When I try to do that, I get the error "Cross-thread operation not valid:
            Control 'TreeView1' accessed from a thread other than the thread it was
            created on"
            >
            I seem to have read something about that poblem before, but can't remember
            if there's a solution to it...
            >
            Please advice asap,
            Johnny J.
            Put in the a place before you call the background compontent to start
            checkforillegal crosscall = false //im not in front the vs but the
            property is something like that

            check that the doesnt fire only on debug

            Comment

            • Peter Duniho

              #7
              Re: Backgroundworke r and controls

              Horacio Nuñez Hernández wrote:
              Put in the a place before you call the background compontent to start
              checkforillegal crosscall = false //im not in front the vs but the
              property is something like that
              No. Do NOT do that. All that does is turn off the error detection.
              The error still exists.

              The first reply from Armin was the best: use BeginInvoke() (or Invoke())
              to run code that updates the UI. Yes, that will cause the code (just
              the code in the invoked method) to run in the main thread. That's the
              whole point, and is exactly what needs to be done.

              Pete

              Comment

              • Peter Duniho

                #8
                Re: Backgroundworke r and controls

                Johnny Jörgensen wrote:
                But won't that be the same thread as my main thread???
                Yes, it will. That's the point.

                To be clear: only the code that does the actual updating of the control
                needs to be called via Invoke() or BeginInvoke(). Armin isn't
                suggesting that you call _all_ of the BackgroundWorke r code via
                Invoke(). Just the code that needs to manipulate your UI elements, such
                as the TreeView control being updated.

                Pete

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Backgroundworke r and controls

                  Horacio Nuñez Hernández <hnh12358@gmail .comwrote:
                  Put in the a place before you call the background compontent to start
                  checkforillegal crosscall = false //im not in front the vs but the
                  property is something like that

                  check that the doesnt fire only on debug
                  The whole point of the exception is to show that you're doing something
                  wrong. You shouldn't access a control from any thread other than the
                  one running its message loop.

                  Setting the property to false just hides the problem temporarily
                  instead of solving it.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too

                  Comment

                  • mpetrotta@gmail.com

                    #10
                    Re: Backgroundworke r and controls

                    On Aug 1, 4:47 am, rowe_newsgroups <rowe_em...@yah oo.comwrote:
                    But won't that be the same thread as my main thread???
                    >
                    Yes.
                    >
                    The problem is that the background worker's thread is not your main
                    thread, which is why you need to use BeginInvoke to delegate control
                    back to the main thread.
                    .... and just to be clear, if you want to call a member of your
                    control, you *must* do it on your UI ("main") thread. More precisely,
                    you must call members of a control from the thread on which that
                    control was created. This is a requirement specific to WinForms
                    controls.

                    More information here:
                    Learn how to use a background thread to search for files in Windows Forms, by means of Visual Basic and C# code samples.


                    (technically, it's not a requirement, just a really, really good idea,
                    one that's enforced by a VisualStudio MDA exception. Windows controls
                    are not thread-safe in that way, and you'll get unpredictable,
                    intermittent errors if you try to work around it.)

                    Michael

                    Comment

                    • Cor Ligthert[MVP]

                      #11
                      Re: Backgroundworke r and controls

                      Jon,

                      Snip
                      >The whole point of the exception is to show that you're doing something
                      >wrong.
                      I won't tell it in this way, more "There is something going wrong". By
                      instance when a server goes down then this can be catched during the proces.
                      Better in my eyes it is a must to catch this, however that has nothing to do
                      with "That you are doing something wrong""

                      I know that it is just an expression, however in my eyes should this be used
                      more for the exception instead of catching program errors, what I have seen
                      is done to much. The last in fact in the same way as Peter writes this.

                      Cor

                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Backgroundworke r and controls

                        Cor Ligthert[MVP] <notmyfirstname @planet.nlwrote :
                        Snip
                        The whole point of the exception is to show that you're doing something
                        wrong.
                        >
                        I won't tell it in this way, more "There is something going wrong". By
                        instance when a server goes down then this can be catched during the proces.
                        Better in my eyes it is a must to catch this, however that has nothing to do
                        with "That you are doing something wrong""
                        >
                        I know that it is just an expression, however in my eyes should this be used
                        more for the exception instead of catching program errors, what I have seen
                        is done to much. The last in fact in the same way as Peter writes this.
                        No, this exception is *specifically* aimed at developers to tell them
                        that they're doing something wrong. It's not there to let you do clean-
                        up etc - it's pointing out a bug.

                        That's why by default it only occurs in debug mode. If it were there to
                        allow clean-up etc in the case of failure, it would happen in release
                        mode too.

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                        If replying to the group, please do not mail me too

                        Comment

                        • Peter Duniho

                          #13
                          Re: Backgroundworke r and controls

                          Cor Ligthert[MVP] wrote:
                          Jon,
                          >
                          Snip
                          >The whole point of the exception is to show that you're doing something
                          >wrong.
                          >
                          I won't tell it in this way, more "There is something going wrong". By
                          instance when a server goes down then this can be catched during the
                          proces. Better in my eyes it is a must to catch this, however that has
                          nothing to do with "That you are doing something wrong""
                          Perhaps what you write makes sense for exceptions generally. However,
                          this particular exception very much is about "you're doing something
                          wrong". That is, it doesn't catch an actual "something going wrong".
                          The code may in fact work much of the time, even when this exception occurs.

                          The MDA exception is in fact specifically saying "you are doing
                          something wrong".

                          Pete

                          Comment

                          • Cor Ligthert[MVP]

                            #14
                            Re: Backgroundworke r and controls

                            Jon and Peter,

                            My excuse, I was only looking at your replies.

                            Cor

                            "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
                            news:MPG.211b7d aa781875f934d@m snews.microsoft .com...
                            Cor Ligthert[MVP] <notmyfirstname @planet.nlwrote :
                            >Snip
                            >The whole point of the exception is to show that you're doing something
                            >wrong.
                            >>
                            >I won't tell it in this way, more "There is something going wrong". By
                            >instance when a server goes down then this can be catched during the
                            >proces.
                            >Better in my eyes it is a must to catch this, however that has nothing to
                            >do
                            >with "That you are doing something wrong""
                            >>
                            >I know that it is just an expression, however in my eyes should this be
                            >used
                            >more for the exception instead of catching program errors, what I have
                            >seen
                            >is done to much. The last in fact in the same way as Peter writes this.
                            >
                            No, this exception is *specifically* aimed at developers to tell them
                            that they're doing something wrong. It's not there to let you do clean-
                            up etc - it's pointing out a bug.
                            >
                            That's why by default it only occurs in debug mode. If it were there to
                            allow clean-up etc in the case of failure, it would happen in release
                            mode too.
                            >
                            --
                            Jon Skeet - <skeet@pobox.co m>
                            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                            If replying to the group, please do not mail me too

                            Comment

                            Working...