Reporting progress from worker thread

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

    #1

    Reporting progress from worker thread

    I have a worker thread doing some long calculation and I'd like to report
    progress (percent done,...). One easy way it to call Control.Invoke( ). But
    is there another way of doing this without using controls that are
    associated with window handles?

    regards
    Tomaz


  • Gordon Smith \(eMVP\)

    #2
    Re: Reporting progress from worker thread

    Tomaz Koritnik wrote:[color=blue]
    > I have a worker thread doing some long calculation and I'd like to
    > report progress (percent done,...). One easy way it to call
    > Control.Invoke( ). But is there another way of doing this without
    > using controls that are associated with window handles?
    >
    > regards
    > Tomaz[/color]

    There are a variety of ways. One simple way could be to update stats in an
    object that both threads have access to whenever it's convienient for the
    worker thread to do so. The foreground thread could then display results
    (by reading stats from that object) to the user on a timed basis (update the
    UI once every N seconds, etc.). You'd want to control access to
    updating/reading that object of course.

    --
    Gordon Smith (eMVP)
    -- Avnet Applied Computing Solutions


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Reporting progress from worker thread

      Tomaz,

      If you want to have some sort of notification that is visual, then
      ultimately, at some level, you will have to call the Invoke method to make
      the call to update the UI.

      You can have your component fire events to indicate progress, however,
      the handlers will have to eventually call Invoke because the event being
      fired is not on the UI.

      If you check out Juval Lowy's book, "Programmin g .NET Components", there
      is a section on events and multithreading, along with a utility class
      (EventsHelper) which will take an implementation of ISynchronizeInv oke and
      perform the event firing on the appropriate thread.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Tomaz Koritnik" <nospam@nospam. com> wrote in message
      news:jakpe.1256 1$F6.2667014@ne ws.siol.net...[color=blue]
      >I have a worker thread doing some long calculation and I'd like to report
      >progress (percent done,...). One easy way it to call Control.Invoke( ). But
      >is there another way of doing this without using controls that are
      >associated with window handles?
      >
      > regards
      > Tomaz
      >[/color]


      Comment

      • Tomaz Koritnik

        #4
        Re: Reporting progress from worker thread

        Hi

        This pull-model is just what I thought about too and it's a great idea. Stat
        object would use critical section to support multithreading and main thread
        would get data from info object using a timer with speed of 1Hz.

        regards
        Tomaz

        "Gordon Smith (eMVP)" <Gordon.Smith@n ospam.avnet.com > wrote in message
        news:%23sxTEG4a FHA.3620@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Tomaz Koritnik wrote:[color=green]
        >> I have a worker thread doing some long calculation and I'd like to
        >> report progress (percent done,...). One easy way it to call
        >> Control.Invoke( ). But is there another way of doing this without
        >> using controls that are associated with window handles?
        >>
        >> regards
        >> Tomaz[/color]
        >
        > There are a variety of ways. One simple way could be to update stats in
        > an object that both threads have access to whenever it's convienient for
        > the worker thread to do so. The foreground thread could then display
        > results (by reading stats from that object) to the user on a timed basis
        > (update the UI once every N seconds, etc.). You'd want to control access
        > to updating/reading that object of course.
        >
        > --
        > Gordon Smith (eMVP)
        > -- Avnet Applied Computing Solutions
        >[/color]


        Comment

        • Tomaz Koritnik

          #5
          Re: Reporting progress from worker thread

          Hi

          Thanks for answering. Is there a simple way to use messages (like in Win32)
          for communication? I will also check the Juval Lowy's book because I found
          it very interesting.

          regards
          Tomaz


          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:%23BJbeH4a FHA.2444@TK2MSF TNGP15.phx.gbl. ..[color=blue]
          > Tomaz,
          >
          > If you want to have some sort of notification that is visual, then
          > ultimately, at some level, you will have to call the Invoke method to make
          > the call to update the UI.
          >
          > You can have your component fire events to indicate progress, however,
          > the handlers will have to eventually call Invoke because the event being
          > fired is not on the UI.
          >
          > If you check out Juval Lowy's book, "Programmin g .NET Components",
          > there is a section on events and multithreading, along with a utility
          > class (EventsHelper) which will take an implementation of
          > ISynchronizeInv oke and perform the event firing on the appropriate thread.
          >
          > Hope this helps.
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Tomaz Koritnik" <nospam@nospam. com> wrote in message
          > news:jakpe.1256 1$F6.2667014@ne ws.siol.net...[color=green]
          >>I have a worker thread doing some long calculation and I'd like to report
          >>progress (percent done,...). One easy way it to call Control.Invoke( ). But
          >>is there another way of doing this without using controls that are
          >>associated with window handles?
          >>
          >> regards
          >> Tomaz
          >>[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Reporting progress from worker thread

            Tomaz,

            Why would you want to use messages? It's much easier to just declare a
            delegate type and then attach the method you want to call to it, and pass it
            (with any parameters) to the Invoke method.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Tomaz Koritnik" <nospam@nospam. com> wrote in message
            news:gUkpe.1256 4$F6.2667285@ne ws.siol.net...[color=blue]
            > Hi
            >
            > Thanks for answering. Is there a simple way to use messages (like in
            > Win32) for communication? I will also check the Juval Lowy's book because
            > I found it very interesting.
            >
            > regards
            > Tomaz
            >
            >
            > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
            > in message news:%23BJbeH4a FHA.2444@TK2MSF TNGP15.phx.gbl. ..[color=green]
            >> Tomaz,
            >>
            >> If you want to have some sort of notification that is visual, then
            >> ultimately, at some level, you will have to call the Invoke method to
            >> make the call to update the UI.
            >>
            >> You can have your component fire events to indicate progress, however,
            >> the handlers will have to eventually call Invoke because the event being
            >> fired is not on the UI.
            >>
            >> If you check out Juval Lowy's book, "Programmin g .NET Components",
            >> there is a section on events and multithreading, along with a utility
            >> class (EventsHelper) which will take an implementation of
            >> ISynchronizeInv oke and perform the event firing on the appropriate
            >> thread.
            >>
            >> Hope this helps.
            >>
            >>
            >> --
            >> - Nicholas Paldino [.NET/C# MVP]
            >> - mvp@spam.guard. caspershouse.co m
            >>
            >> "Tomaz Koritnik" <nospam@nospam. com> wrote in message
            >> news:jakpe.1256 1$F6.2667014@ne ws.siol.net...[color=darkred]
            >>>I have a worker thread doing some long calculation and I'd like to report
            >>>progress (percent done,...). One easy way it to call Control.Invoke( ).
            >>>But is there another way of doing this without using controls that are
            >>>associated with window handles?
            >>>
            >>> regards
            >>> Tomaz
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            Working...