A question on threading

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

    A question on threading

    Hello,

    I am writing a program related to image compression in C# (GUI
    Application). In that I have a "heavy, long running" method which
    among other things, calls this.Invalidate (); to make the screen
    refresh to display the changes in image during processing.

    The problem is that, once the program enters this method, the GUI
    Window becomes non-responsive (but GUI gets updated regularly due to
    this.Invalidate ()).

    So, I want the GUI to stay responsive while executing this method.

    I tried running the method in another thread but then I cannot call
    this.Invalidate () because "this" is in parent thread, and it throws
    error.

    How can I keep my window responsive while running this method? Any
    ideas?
  • Mugunth

    #2
    Re: A question on threading

    On Mar 2, 8:38 pm, Rahul <rahu...@gmail. comwrote:
    Hello,
    >
    I am writing a program related to image compression in C# (GUI
    Application). In that I have a "heavy, long running" method which
    among other things, calls this.Invalidate (); to make the screen
    refresh to display the changes in image during processing.
    >
    The problem is that, once the program enters this method, the GUI
    Window becomes non-responsive (but GUI gets updated regularly due to
    this.Invalidate ()).
    >
    So, I want the GUI to stay responsive while executing this method.
    >
    I tried running the method in another thread but then I cannot call
    this.Invalidate () because "this" is in parent thread, and it throws
    error.
    >
    How can I keep my window responsive while running this method? Any
    ideas?
    Try using a backgroundworke r.

    Comment

    • Peter Webb

      #3
      Re: A question on threading


      "Rahul" <rahuldj@gmail. comwrote in message
      news:00d8eb3f-a115-4527-8052-1dc6139b5ab3@e6 g2000prf.google groups.com...
      Hello,
      >
      I am writing a program related to image compression in C# (GUI
      Application). In that I have a "heavy, long running" method which
      among other things, calls this.Invalidate (); to make the screen
      refresh to display the changes in image during processing.
      >
      The problem is that, once the program enters this method, the GUI
      Window becomes non-responsive (but GUI gets updated regularly due to
      this.Invalidate ()).
      >
      So, I want the GUI to stay responsive while executing this method.
      >
      I tried running the method in another thread but then I cannot call
      this.Invalidate () because "this" is in parent thread, and it throws
      error.
      >
      How can I keep my window responsive while running this method? Any
      ideas?
      Application.DoE vents() will force your thread to process messages from the
      UI. Its probably not the right way to do it, but it will very probably work.
      Just stick it in somewhere you expect to be called at least 50 times per
      second and the UI should work just fine.




      Comment

      • Rahul

        #4
        Re: A question on threading

        On Mar 2, 4:47 am, Mugunth <mugunth.ku...@ gmail.comwrote:
        On Mar 2, 8:38 pm, Rahul <rahu...@gmail. comwrote:
        >
        >
        >
        Hello,
        >
        I am writing a program related to image compression in C# (GUI
        Application). In that I have a "heavy, long running" method which
        among other things, calls this.Invalidate (); to make the screen
        refresh to display the changes in image during processing.
        >
        The problem is that, once the program enters this method, the GUI
        Window becomes non-responsive (but GUI gets updated regularly due to
        this.Invalidate ()).
        >
        So, I want the GUI to stay responsive while executing this method.
        >
        I tried running the method in another thread but then I cannot call
        this.Invalidate () because "this" is in parent thread, and it throws
        error.
        >
        How can I keep my window responsive while running this method? Any
        ideas?
        >
        Try using a backgroundworke r.
        I tried to call the function through BackgroundWorke r.

        The "this.Invalidat e()" runs properly. But this.Update() (which is the
        next statement) does not work.
        It throws the same exception "Cross-thread operation not valid:
        Control '<FormName>' accessed from a thread other than the thread it
        was created on."

        this.Update() is necessary to ensure that GUI gets updated quickly, so
        as to make the changes visible in pseudo-real-time.

        Thanks for the reply.

        Any other ideas?

        Comment

        • Rahul

          #5
          Re: A question on threading

          On Mar 2, 5:10 am, "Peter Webb" <webbfam...@DIE SPAMDIEoptusnet .com.au>
          wrote:
          "Rahul" <rahu...@gmail. comwrote in message
          >
          news:00d8eb3f-a115-4527-8052-1dc6139b5ab3@e6 g2000prf.google groups.com...
          >
          >
          >
          Hello,
          >
          I am writing a program related to image compression in C# (GUI
          Application). In that I have a "heavy, long running" method which
          among other things, calls this.Invalidate (); to make the screen
          refresh to display the changes in image during processing.
          >
          The problem is that, once the program enters this method, the GUI
          Window becomes non-responsive (but GUI gets updated regularly due to
          this.Invalidate ()).
          >
          So, I want the GUI to stay responsive while executing this method.
          >
          I tried running the method in another thread but then I cannot call
          this.Invalidate () because "this" is in parent thread, and it throws
          error.
          >
          How can I keep my window responsive while running this method? Any
          ideas?
          >
          Application.DoE vents() will force your thread to process messages from the
          UI. Its probably not the right way to do it, but it will very probably work.
          Just stick it in somewhere you expect to be called at least 50 times per
          second and the UI should work just fine.
          Peter,

          A brilliant hack!! Although this might not be the best possible way...
          it gets the job done.

          Unless someone suggests a more "elegant" solution, this would do just
          fine.

          Thanks a lot! :)

          Comment

          • Alfred Myers

            #6
            Re: A question on threading

            Hi,

            The UI only should be updated by the same thread that created the Control's
            windows handle.
            To do that, you can use Control.Invoke which receives a delegate pointing to
            the code you want to execute.
            For the delegate you can use a MethodInvoker such as the following.

            this.Invoke(new MethodInvoker(t his.Update));

            HTH


            "Rahul" <rahuldj@gmail. comwrote in message
            news:54ad76e9-add1-4b33-b8d0-f36eae0961a2@s1 9g2000prg.googl egroups.com...
            On Mar 2, 5:10 am, "Peter Webb" <webbfam...@DIE SPAMDIEoptusnet .com.au>
            wrote:
            >"Rahul" <rahu...@gmail. comwrote in message
            >>
            >news:00d8eb3 f-a115-4527-8052-1dc6139b5ab3@e6 g2000prf.google groups.com...
            >>
            >>
            >>
            Hello,
            >>
            I am writing a program related to image compression in C# (GUI
            Application). In that I have a "heavy, long running" method which
            among other things, calls this.Invalidate (); to make the screen
            refresh to display the changes in image during processing.
            >>
            The problem is that, once the program enters this method, the GUI
            Window becomes non-responsive (but GUI gets updated regularly due to
            this.Invalidate ()).
            >>
            So, I want the GUI to stay responsive while executing this method.
            >>
            I tried running the method in another thread but then I cannot call
            this.Invalidate () because "this" is in parent thread, and it throws
            error.
            >>
            How can I keep my window responsive while running this method? Any
            ideas?
            >>
            >Application.Do Events() will force your thread to process messages from
            >the
            >UI. Its probably not the right way to do it, but it will very probably
            >work.
            >Just stick it in somewhere you expect to be called at least 50 times per
            >second and the UI should work just fine.
            >
            Peter,
            >
            A brilliant hack!! Although this might not be the best possible way...
            it gets the job done.
            >
            Unless someone suggests a more "elegant" solution, this would do just
            fine.
            >
            Thanks a lot! :)

            Comment

            • Peter Duniho

              #7
              Re: A question on threading

              On Sun, 02 Mar 2008 05:28:01 -0800, Rahul <rahuldj@gmail. comwrote:
              [...]
              >Application.Do Events() will force your thread to process messages from
              >the
              >UI. [...]
              >
              Peter,
              >
              A brilliant hack!! Although this might not be the best possible way...
              it gets the job done.
              >
              Unless someone suggests a more "elegant" solution, this would do just
              fine.
              Do NOT accept that advice. It is the worst way to deal with this issue.

              As mentioned by others, you can use more appropriate techniques such as
              using a BackgroundWorke r in conjunction with the ProgressChanged event, or
              simply calling Control.Invoke( ) or Control.BeginIn voke() from your worker
              thread to update the UI.

              Note also that there's no need to call Control.Update( ) directly if you're
              doing things correctly. For a control where you're doing your own
              painting, simply calling Invalidate() will have the intended effect, and
              of course for other controls, just changing their state will result in the
              control itself doing the necessary invalidation.

              I've yet to see an application that call Application.DoE vents() that
              wasn't just plain broken. I doubt they exist and I'm certain, given your
              description, that it's absolutely the wrong way to approach the issue in
              your case.

              Pete

              Comment

              • Rahul

                #8
                Re: A question on threading

                On Mar 2, 6:12 am, "Alfred Myers" <alfred_my...@h otmail.comwrote :
                Hi,
                >
                The UI only should be updated by the same thread that created the Control's
                windows handle.
                To do that, you can use Control.Invoke which receives a delegate pointing to
                the code you want to execute.
                For the delegate you can use a MethodInvoker such as the following.
                >
                this.Invoke(new MethodInvoker(t his.Update));
                >
                HTH
                >
                "Rahul" <rahu...@gmail. comwrote in message
                >
                news:54ad76e9-add1-4b33-b8d0-f36eae0961a2@s1 9g2000prg.googl egroups.com...
                >
                On Mar 2, 5:10 am, "Peter Webb" <webbfam...@DIE SPAMDIEoptusnet .com.au>
                wrote:
                "Rahul" <rahu...@gmail. comwrote in message
                >
                >news:00d8eb3 f-a115-4527-8052-1dc6139b5ab3@e6 g2000prf.google groups.com...
                >
                Hello,
                >
                I am writing a program related to image compression in C# (GUI
                Application). In that I have a "heavy, long running" method which
                among other things, calls this.Invalidate (); to make the screen
                refresh to display the changes in image during processing.
                >
                The problem is that, once the program enters this method, the GUI
                Window becomes non-responsive (but GUI gets updated regularly due to
                this.Invalidate ()).
                >
                So, I want the GUI to stay responsive while executing this method.
                >
                I tried running the method in another thread but then I cannot call
                this.Invalidate () because "this" is in parent thread, and it throws
                error.
                >
                How can I keep my window responsive while running this method? Any
                ideas?
                >
                Application.DoE vents() will force your thread to process messages from
                the
                UI. Its probably not the right way to do it, but it will very probably
                work.
                Just stick it in somewhere you expect to be called at least 50 times per
                second and the UI should work just fine.
                >
                Peter,
                >
                A brilliant hack!! Although this might not be the best possible way...
                it gets the job done.
                >
                Unless someone suggests a more "elegant" solution, this would do just
                fine.
                >
                Thanks a lot! :)
                Hello Alfred,

                I will definitely try this approach.

                Thanks a lot.

                Comment

                Working...