Communicating between a main thread and a worker thread?

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

    #1

    Communicating between a main thread and a worker thread?

    I have an object that spawns a worker thread to process one of its methods.
    That method processes methods on a collection of other objects. During this
    processing, a user may request to cancel the entire operation. I could
    request abort on the worker thread, but that is a) potentially messy, and b)
    not guaranteed to take immediate effect anyway. I would rather have some way
    of allowing the main thread to tell the worker thread that it should stop.
    The code being executed by the worker can then check periodically for a stop
    request. An added complication is that this should be compatible with
    ASP.NET, meaning that any use of static variables needs special
    consideration.

    I'd be grateful for any advice.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Communicating between a main thread and a worker thread?

    Joe,

    First off, if you are using ASP.NET, then you really have no reason to
    spawn another thread. Your page is not going to be visible while processing
    anyways, so you have no need for the extra thread.

    If you are using this in a richer client app, then what you want to do
    is have a field in the class the method is on which indicates whether or not
    it should stop processing. You need to protect access to this field with a
    lock statement, so that it is not corrupted if one thread tries to read and
    one thread tries to write to it.

    Once you have that, check through each iteration of the loop, and if the
    flag is true, then stop processing the look, and exit the method graciously.

    Hope this helps.


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

    "Joe Jax" <jj@nowhere.com > wrote in message
    news:u6j2DjvEGH A.272@TK2MSFTNG P10.phx.gbl...[color=blue]
    >I have an object that spawns a worker thread to process one of its methods.
    >That method processes methods on a collection of other objects. During this
    >processing, a user may request to cancel the entire operation. I could
    >request abort on the worker thread, but that is a) potentially messy, and
    >b) not guaranteed to take immediate effect anyway. I would rather have some
    >way of allowing the main thread to tell the worker thread that it should
    >stop. The code being executed by the worker can then check periodically for
    >a stop request. An added complication is that this should be compatible
    >with ASP.NET, meaning that any use of static variables needs special
    >consideratio n.
    >
    > I'd be grateful for any advice.
    >[/color]


    Comment

    • Stoitcho Goutsev \(100\) [C# MVP]

      #3
      Re: Communicating between a main thread and a worker thread?

      Beside what Nicholas suggested I'd recomend reading through the following
      MSDN article:
      http://msdn2.microsoft.com/en-us/library/ts553s52.aspx

      There are two solutions one is for .NET 2.0 only and the other is general
      and works on all versions.


      --

      Stoitcho Goutsev (100)

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:%234uDHxvE GHA.1540@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Joe,
      >
      > First off, if you are using ASP.NET, then you really have no reason to
      > spawn another thread. Your page is not going to be visible while
      > processing anyways, so you have no need for the extra thread.
      >
      > If you are using this in a richer client app, then what you want to do
      > is have a field in the class the method is on which indicates whether or
      > not it should stop processing. You need to protect access to this field
      > with a lock statement, so that it is not corrupted if one thread tries to
      > read and one thread tries to write to it.
      >
      > Once you have that, check through each iteration of the loop, and if
      > the flag is true, then stop processing the look, and exit the method
      > graciously.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Joe Jax" <jj@nowhere.com > wrote in message
      > news:u6j2DjvEGH A.272@TK2MSFTNG P10.phx.gbl...[color=green]
      >>I have an object that spawns a worker thread to process one of its
      >>methods. That method processes methods on a collection of other objects.
      >>During this processing, a user may request to cancel the entire operation.
      >>I could request abort on the worker thread, but that is a) potentially
      >>messy, and b) not guaranteed to take immediate effect anyway. I would
      >>rather have some way of allowing the main thread to tell the worker thread
      >>that it should stop. The code being executed by the worker can then check
      >>periodicall y for a stop request. An added complication is that this should
      >>be compatible with ASP.NET, meaning that any use of static variables needs
      >>special consideration.
      >>
      >> I'd be grateful for any advice.
      >>[/color]
      >
      >[/color]


      Comment

      • Rick

        #4
        Re: Communicating between a main thread and a worker thread?

        Thanks, I'll have a look at that.

        "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
        news:O39GFmwEGH A.644@TK2MSFTNG P09.phx.gbl...[color=blue]
        > Beside what Nicholas suggested I'd recomend reading through the following
        > MSDN article:
        > http://msdn2.microsoft.com/en-us/library/ts553s52.aspx
        >
        > There are two solutions one is for .NET 2.0 only and the other is general
        > and works on all versions.
        >
        >
        > --
        >
        > Stoitcho Goutsev (100)
        >
        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
        > in message news:%234uDHxvE GHA.1540@TK2MSF TNGP10.phx.gbl. ..[color=green]
        >> Joe,
        >>
        >> First off, if you are using ASP.NET, then you really have no reason to
        >> spawn another thread. Your page is not going to be visible while
        >> processing anyways, so you have no need for the extra thread.
        >>
        >> If you are using this in a richer client app, then what you want to do
        >> is have a field in the class the method is on which indicates whether or
        >> not it should stop processing. You need to protect access to this field
        >> with a lock statement, so that it is not corrupted if one thread tries to
        >> read and one thread tries to write to it.
        >>
        >> Once you have that, check through each iteration of the loop, and if
        >> the flag is true, then stop processing the look, and exit the method
        >> graciously.
        >>
        >> Hope this helps.
        >>
        >>
        >> --
        >> - Nicholas Paldino [.NET/C# MVP]
        >> - mvp@spam.guard. caspershouse.co m
        >>
        >> "Joe Jax" <jj@nowhere.com > wrote in message
        >> news:u6j2DjvEGH A.272@TK2MSFTNG P10.phx.gbl...[color=darkred]
        >>>I have an object that spawns a worker thread to process one of its
        >>>methods. That method processes methods on a collection of other objects.
        >>>During this processing, a user may request to cancel the entire
        >>>operation. I could request abort on the worker thread, but that is a)
        >>>potentiall y messy, and b) not guaranteed to take immediate effect anyway.
        >>>I would rather have some way of allowing the main thread to tell the
        >>>worker thread that it should stop. The code being executed by the worker
        >>>can then check periodically for a stop request. An added complication is
        >>>that this should be compatible with ASP.NET, meaning that any use of
        >>>static variables needs special consideration.
        >>>
        >>> I'd be grateful for any advice.
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Rick

          #5
          Re: Communicating between a main thread and a worker thread?

          The scenario is this: a user kicks off a report from a web page. The report
          begins to run but may take a minute or two to generate. Meanwhile the user
          decides to cancel the report. If I run the report in the main thread it will
          be tied up and unable to process a request to cancel. I think. :-)

          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:%234uDHxvE GHA.1540@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          > Joe,
          >
          > First off, if you are using ASP.NET, then you really have no reason to
          > spawn another thread. Your page is not going to be visible while
          > processing anyways, so you have no need for the extra thread.
          >
          > If you are using this in a richer client app, then what you want to do
          > is have a field in the class the method is on which indicates whether or
          > not it should stop processing. You need to protect access to this field
          > with a lock statement, so that it is not corrupted if one thread tries to
          > read and one thread tries to write to it.
          >
          > Once you have that, check through each iteration of the loop, and if
          > the flag is true, then stop processing the look, and exit the method
          > graciously.
          >
          > Hope this helps.
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Joe Jax" <jj@nowhere.com > wrote in message
          > news:u6j2DjvEGH A.272@TK2MSFTNG P10.phx.gbl...[color=green]
          >>I have an object that spawns a worker thread to process one of its
          >>methods. That method processes methods on a collection of other objects.
          >>During this processing, a user may request to cancel the entire operation.
          >>I could request abort on the worker thread, but that is a) potentially
          >>messy, and b) not guaranteed to take immediate effect anyway. I would
          >>rather have some way of allowing the main thread to tell the worker thread
          >>that it should stop. The code being executed by the worker can then check
          >>periodicall y for a stop request. An added complication is that this should
          >>be compatible with ASP.NET, meaning that any use of static variables needs
          >>special consideration.
          >>
          >> I'd be grateful for any advice.
          >>[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Communicating between a main thread and a worker thread?

            Rick,

            What does it matter? The user is still waiting for the request to
            complete. It doesn't matter, in the ASP space, since it is a completely
            differently way of processing things. To perform an asynchronous task, you
            need to actually kick the task off, but it is up to the client (browser) to
            refresh itself to check on the status of that task.

            Hope this helps.


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

            "Rick" <rick@nospam.co m> wrote in message
            news:eh$7sY1EGH A.1028@TK2MSFTN GP11.phx.gbl...[color=blue]
            > The scenario is this: a user kicks off a report from a web page. The
            > report begins to run but may take a minute or two to generate. Meanwhile
            > the user decides to cancel the report. If I run the report in the main
            > thread it will be tied up and unable to process a request to cancel. I
            > think. :-)
            >
            > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
            > in message news:%234uDHxvE GHA.1540@TK2MSF TNGP10.phx.gbl. ..[color=green]
            >> Joe,
            >>
            >> First off, if you are using ASP.NET, then you really have no reason to
            >> spawn another thread. Your page is not going to be visible while
            >> processing anyways, so you have no need for the extra thread.
            >>
            >> If you are using this in a richer client app, then what you want to do
            >> is have a field in the class the method is on which indicates whether or
            >> not it should stop processing. You need to protect access to this field
            >> with a lock statement, so that it is not corrupted if one thread tries to
            >> read and one thread tries to write to it.
            >>
            >> Once you have that, check through each iteration of the loop, and if
            >> the flag is true, then stop processing the look, and exit the method
            >> graciously.
            >>
            >> Hope this helps.
            >>
            >>
            >> --
            >> - Nicholas Paldino [.NET/C# MVP]
            >> - mvp@spam.guard. caspershouse.co m
            >>
            >> "Joe Jax" <jj@nowhere.com > wrote in message
            >> news:u6j2DjvEGH A.272@TK2MSFTNG P10.phx.gbl...[color=darkred]
            >>>I have an object that spawns a worker thread to process one of its
            >>>methods. That method processes methods on a collection of other objects.
            >>>During this processing, a user may request to cancel the entire
            >>>operation. I could request abort on the worker thread, but that is a)
            >>>potentiall y messy, and b) not guaranteed to take immediate effect anyway.
            >>>I would rather have some way of allowing the main thread to tell the
            >>>worker thread that it should stop. The code being executed by the worker
            >>>can then check periodically for a stop request. An added complication is
            >>>that this should be compatible with ASP.NET, meaning that any use of
            >>>static variables needs special consideration.
            >>>
            >>> I'd be grateful for any advice.
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Rick

              #7
              Re: Communicating between a main thread and a worker thread?

              The way it works at the moment is that the client asks for the report, and
              during the postback the server kicks off the report in a worker thread
              (storing the report object in session state), leaving the main thread to
              complete the postback and then die. The postback then includes client-side
              script to instruct the browser to refresh every 3 seconds and poll the
              status of the report. During this time, the user may click a cancel button,
              causing a post-back. During this postback I want to stop the report from
              processing any further. I therefore need some way of passing a message to
              the running worker thread to ask it to stop ASAP.

              One solution I have come up with is to hold a static list of all active
              report worker threads. When I start a worker thread I can store the unique
              thread ID both in this list and in the report object (which, remember, is
              held in the session state). Then when I request to cancel the report, I can
              remove the entry from the list of active threads. If each worker
              periodically checks the list, it can then take action when its own entry
              disappears.

              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
              message news:eKU0es8EGH A.1288@TK2MSFTN GP09.phx.gbl...[color=blue]
              > Rick,
              >
              > What does it matter? The user is still waiting for the request to
              > complete. It doesn't matter, in the ASP space, since it is a completely
              > differently way of processing things. To perform an asynchronous task,
              > you need to actually kick the task off, but it is up to the client
              > (browser) to refresh itself to check on the status of that task.
              >
              > Hope this helps.
              >
              >
              > --
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >
              > "Rick" <rick@nospam.co m> wrote in message
              > news:eh$7sY1EGH A.1028@TK2MSFTN GP11.phx.gbl...[color=green]
              >> The scenario is this: a user kicks off a report from a web page. The
              >> report begins to run but may take a minute or two to generate. Meanwhile
              >> the user decides to cancel the report. If I run the report in the main
              >> thread it will be tied up and unable to process a request to cancel. I
              >> think. :-)
              >>
              >> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
              >> in message news:%234uDHxvE GHA.1540@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
              >>> Joe,
              >>>
              >>> First off, if you are using ASP.NET, then you really have no reason
              >>> to spawn another thread. Your page is not going to be visible while
              >>> processing anyways, so you have no need for the extra thread.
              >>>
              >>> If you are using this in a richer client app, then what you want to
              >>> do is have a field in the class the method is on which indicates whether
              >>> or not it should stop processing. You need to protect access to this
              >>> field with a lock statement, so that it is not corrupted if one thread
              >>> tries to read and one thread tries to write to it.
              >>>
              >>> Once you have that, check through each iteration of the loop, and if
              >>> the flag is true, then stop processing the look, and exit the method
              >>> graciously.
              >>>
              >>> Hope this helps.
              >>>
              >>>
              >>> --
              >>> - Nicholas Paldino [.NET/C# MVP]
              >>> - mvp@spam.guard. caspershouse.co m
              >>>
              >>> "Joe Jax" <jj@nowhere.com > wrote in message
              >>> news:u6j2DjvEGH A.272@TK2MSFTNG P10.phx.gbl...
              >>>>I have an object that spawns a worker thread to process one of its
              >>>>methods. That method processes methods on a collection of other objects.
              >>>>During this processing, a user may request to cancel the entire
              >>>>operation . I could request abort on the worker thread, but that is a)
              >>>>potential ly messy, and b) not guaranteed to take immediate effect
              >>>>anyway. I would rather have some way of allowing the main thread to tell
              >>>>the worker thread that it should stop. The code being executed by the
              >>>>worker can then check periodically for a stop request. An added
              >>>>complicatio n is that this should be compatible with ASP.NET, meaning
              >>>>that any use of static variables needs special consideration.
              >>>>
              >>>> I'd be grateful for any advice.
              >>>>
              >>>
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              Working...