Thread question (doubt)

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

    Thread question (doubt)

    I dispensed some advice and its bugging me that it may not be 100% accurate.

    Worker threads should not touch main thread objects. Everybody knows that
    but if you pass a reference to a form object to a worker thread and you
    touch that form on the worker thread. All is well.

    Is that right? Will all really be well? It's a reference to the form object.
    The worker thread is still modifying the main threads object, reference or
    not, without using control invoke - that is, it isn't getting modified from
    the thread owning the object in the correct way.

    I'm not sure I now agree that this is safe. Infact I now realize that this
    is absolutely wrong.
    Any help here?

    --


    -----------
    Got TidBits?
    Get it here: www.networkip.net/tidbits


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Thread question (doubt)

    Alvin,


    It is wrong. For a form object, even if access to the object was
    synchronized (which it is not), the windows handle and all operations on it
    (which the form encapsulates) requires that you make the call on the thread
    where the element was created.

    If you never run into problems in your scenario, then that's great, but
    if you do, it's something that you could have avoided now by using a call to
    Invoke.

    Hope this helps.


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

    "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
    message news:%23zjChZko DHA.2364@TK2MSF TNGP11.phx.gbl. ..[color=blue]
    > I dispensed some advice and its bugging me that it may not be 100%[/color]
    accurate.[color=blue]
    >
    > Worker threads should not touch main thread objects. Everybody knows that
    > but if you pass a reference to a form object to a worker thread and you
    > touch that form on the worker thread. All is well.
    >
    > Is that right? Will all really be well? It's a reference to the form[/color]
    object.[color=blue]
    > The worker thread is still modifying the main threads object, reference or
    > not, without using control invoke - that is, it isn't getting modified[/color]
    from[color=blue]
    > the thread owning the object in the correct way.
    >
    > I'm not sure I now agree that this is safe. Infact I now realize that this
    > is absolutely wrong.
    > Any help here?
    >
    > --
    >
    >
    > -----------
    > Got TidBits?
    > Get it here: www.networkip.net/tidbits
    >
    >[/color]


    Comment

    • 100

      #3
      Re: Thread question (doubt)

      Hi Alvin,

      You can access and modify objects created in other threads as long as you
      make sure that this is thread safe. What is thread safe in some degree
      depends on your very application. Some times it means that multiple threads
      can read, but only one can write when nobody is reading . In other cases
      only one thread can read and so on.
      But only the UI thread created the UI control can use methods and properties
      that cause sending messages to its underlaying win control. This is Windows
      OS issue. If someone ports WindowsForms for other platform it maight be not
      required. I'm even not sure if it is necessary for CF.
      However, because it is not so obvious which methods and properties send
      messages usually the advice is *don't modify a control form thread different
      from the one owning the undelaying window handle.

      B\rgds
      100

      "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
      message news:%23zjChZko DHA.2364@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > I dispensed some advice and its bugging me that it may not be 100%[/color]
      accurate.[color=blue]
      >
      > Worker threads should not touch main thread objects. Everybody knows that
      > but if you pass a reference to a form object to a worker thread and you
      > touch that form on the worker thread. All is well.
      >
      > Is that right? Will all really be well? It's a reference to the form[/color]
      object.[color=blue]
      > The worker thread is still modifying the main threads object, reference or
      > not, without using control invoke - that is, it isn't getting modified[/color]
      from[color=blue]
      > the thread owning the object in the correct way.
      >
      > I'm not sure I now agree that this is safe. Infact I now realize that this
      > is absolutely wrong.
      > Any help here?
      >
      > --
      >
      >
      > -----------
      > Got TidBits?
      > Get it here: www.networkip.net/tidbits
      >
      >[/color]


      Comment

      • Alvin Bruney

        #4
        Re: Thread question (doubt)

        Right, I get that.
        Now, does the same thing apply for session objects - which is where I was
        headed in the first place? Is the instance of the HttpContext object owned
        by the main thread? The line gets a little blury here for me. If the access
        is done within the context of the global object, it is guaranteed to be
        called correctly. (I also need confirmation on that, but I believe this is
        the case).

        But what if you just spawn a worker thread and want to touch session object.
        How would I use a control invoke? Control.Invoke( pass in what here though?)
        the param accepts a method of type delegate not an object.

        Memory failed me, I was actually making the call in global and not on a
        worker thread, but this got my curiosity up.
        [color=blue]
        > If you never run into problems in your scenario, then that's great[/color]
        it would only be a matter of time before that would start failing, like
        right after the product shipped usually.

        regards

        --


        -----------
        Got TidBits?
        Get it here: www.networkip.net/tidbits
        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
        message news:uDmApokoDH A.1960@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Alvin,
        >
        >
        > It is wrong. For a form object, even if access to the object was
        > synchronized (which it is not), the windows handle and all operations on[/color]
        it[color=blue]
        > (which the form encapsulates) requires that you make the call on the[/color]
        thread[color=blue]
        > where the element was created.
        >
        > If you never run into problems in your scenario, then that's great,[/color]
        but[color=blue]
        > if you do, it's something that you could have avoided now by using a call[/color]
        to[color=blue]
        > Invoke.
        >
        > Hope this helps.
        >
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
        > message news:%23zjChZko DHA.2364@TK2MSF TNGP11.phx.gbl. ..[color=green]
        > > I dispensed some advice and its bugging me that it may not be 100%[/color]
        > accurate.[color=green]
        > >
        > > Worker threads should not touch main thread objects. Everybody knows[/color][/color]
        that[color=blue][color=green]
        > > but if you pass a reference to a form object to a worker thread and you
        > > touch that form on the worker thread. All is well.
        > >
        > > Is that right? Will all really be well? It's a reference to the form[/color]
        > object.[color=green]
        > > The worker thread is still modifying the main threads object, reference[/color][/color]
        or[color=blue][color=green]
        > > not, without using control invoke - that is, it isn't getting modified[/color]
        > from[color=green]
        > > the thread owning the object in the correct way.
        > >
        > > I'm not sure I now agree that this is safe. Infact I now realize that[/color][/color]
        this[color=blue][color=green]
        > > is absolutely wrong.
        > > Any help here?
        > >
        > > --
        > >
        > >
        > > -----------
        > > Got TidBits?
        > > Get it here: www.networkip.net/tidbits
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Thread question (doubt)

          Alvin,

          Controls in ASP.NET do not follow the windows model, so you can modify
          them on any thread you like (from what I know). However, you can not access
          the HttpContext in other threads, as I believe that they are associated with
          the threads that the page is processed on. If you want to access this
          information from another thread, then I recommend you pass the HttpContext
          along to that thread.

          Hope this helps.


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

          "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
          message news:uW2rI4koDH A.644@TK2MSFTNG P11.phx.gbl...[color=blue]
          > Right, I get that.
          > Now, does the same thing apply for session objects - which is where I was
          > headed in the first place? Is the instance of the HttpContext object owned
          > by the main thread? The line gets a little blury here for me. If the[/color]
          access[color=blue]
          > is done within the context of the global object, it is guaranteed to be
          > called correctly. (I also need confirmation on that, but I believe this is
          > the case).
          >
          > But what if you just spawn a worker thread and want to touch session[/color]
          object.[color=blue]
          > How would I use a control invoke? Control.Invoke( pass in what here[/color]
          though?)[color=blue]
          > the param accepts a method of type delegate not an object.
          >
          > Memory failed me, I was actually making the call in global and not on a
          > worker thread, but this got my curiosity up.
          >[color=green]
          > > If you never run into problems in your scenario, then that's great[/color]
          > it would only be a matter of time before that would start failing, like
          > right after the product shipped usually.
          >
          > regards
          >
          > --
          >
          >
          > -----------
          > Got TidBits?
          > Get it here: www.networkip.net/tidbits
          > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
          in[color=blue]
          > message news:uDmApokoDH A.1960@TK2MSFTN GP12.phx.gbl...[color=green]
          > > Alvin,
          > >
          > >
          > > It is wrong. For a form object, even if access to the object was
          > > synchronized (which it is not), the windows handle and all operations on[/color]
          > it[color=green]
          > > (which the form encapsulates) requires that you make the call on the[/color]
          > thread[color=green]
          > > where the element was created.
          > >
          > > If you never run into problems in your scenario, then that's great,[/color]
          > but[color=green]
          > > if you do, it's something that you could have avoided now by using a[/color][/color]
          call[color=blue]
          > to[color=green]
          > > Invoke.
          > >
          > > Hope this helps.
          > >
          > >
          > > --
          > > - Nicholas Paldino [.NET/C# MVP]
          > > - mvp@spam.guard. caspershouse.co m
          > >
          > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote[/color][/color]
          in[color=blue][color=green]
          > > message news:%23zjChZko DHA.2364@TK2MSF TNGP11.phx.gbl. ..[color=darkred]
          > > > I dispensed some advice and its bugging me that it may not be 100%[/color]
          > > accurate.[color=darkred]
          > > >
          > > > Worker threads should not touch main thread objects. Everybody knows[/color][/color]
          > that[color=green][color=darkred]
          > > > but if you pass a reference to a form object to a worker thread and[/color][/color][/color]
          you[color=blue][color=green][color=darkred]
          > > > touch that form on the worker thread. All is well.
          > > >
          > > > Is that right? Will all really be well? It's a reference to the form[/color]
          > > object.[color=darkred]
          > > > The worker thread is still modifying the main threads object,[/color][/color][/color]
          reference[color=blue]
          > or[color=green][color=darkred]
          > > > not, without using control invoke - that is, it isn't getting modified[/color]
          > > from[color=darkred]
          > > > the thread owning the object in the correct way.
          > > >
          > > > I'm not sure I now agree that this is safe. Infact I now realize that[/color][/color]
          > this[color=green][color=darkred]
          > > > is absolutely wrong.
          > > > Any help here?
          > > >
          > > > --
          > > >
          > > >
          > > > -----------
          > > > Got TidBits?
          > > > Get it here: www.networkip.net/tidbits
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Alvin Bruney

            #6
            Re: Thread question (doubt)

            That's exactly what I recommended but I guess I want to advance my knowledge
            of the model, how it behaves and what are the limits I can push it to.

            Where can I read more about threading and controls as it applies to webforms
            (not interested in winforms)? Or a book perhaps? My knowledge of this model
            is no longer sufficient to satisfy my curiosity.

            I'm aware that precious little is out there concerning threading in webforms
            while there is a ton on winforms. Even the more widely read books purposely
            shy away from this topic or set aside a line or two to discuss it.

            For example, you said 'you can modify them on any thread you like (from what
            I know)'.

            Well how did you come across that piece of information?
            I know it from getting burned by it. Learning the hardway is too expensive
            so i'd rather read about it and save the frustration for something else.

            regards

            --


            -----------
            Got TidBits?
            Get it here: www.networkip.net/tidbits
            "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
            message news:Om7Y88loDH A.1408@TK2MSFTN GP11.phx.gbl...[color=blue]
            > Alvin,
            >
            > Controls in ASP.NET do not follow the windows model, so you can modify
            > them on any thread you like (from what I know). However, you can not[/color]
            access[color=blue]
            > the HttpContext in other threads, as I believe that they are associated[/color]
            with[color=blue]
            > the threads that the page is processed on. If you want to access this
            > information from another thread, then I recommend you pass the HttpContext
            > along to that thread.
            >
            > Hope this helps.
            >
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - mvp@spam.guard. caspershouse.co m
            >
            > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
            > message news:uW2rI4koDH A.644@TK2MSFTNG P11.phx.gbl...[color=green]
            > > Right, I get that.
            > > Now, does the same thing apply for session objects - which is where I[/color][/color]
            was[color=blue][color=green]
            > > headed in the first place? Is the instance of the HttpContext object[/color][/color]
            owned[color=blue][color=green]
            > > by the main thread? The line gets a little blury here for me. If the[/color]
            > access[color=green]
            > > is done within the context of the global object, it is guaranteed to be
            > > called correctly. (I also need confirmation on that, but I believe this[/color][/color]
            is[color=blue][color=green]
            > > the case).
            > >
            > > But what if you just spawn a worker thread and want to touch session[/color]
            > object.[color=green]
            > > How would I use a control invoke? Control.Invoke( pass in what here[/color]
            > though?)[color=green]
            > > the param accepts a method of type delegate not an object.
            > >
            > > Memory failed me, I was actually making the call in global and not on a
            > > worker thread, but this got my curiosity up.
            > >[color=darkred]
            > > > If you never run into problems in your scenario, then that's great[/color]
            > > it would only be a matter of time before that would start failing, like
            > > right after the product shipped usually.
            > >
            > > regards
            > >
            > > --
            > >
            > >
            > > -----------
            > > Got TidBits?
            > > Get it here: www.networkip.net/tidbits
            > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
            > in[color=green]
            > > message news:uDmApokoDH A.1960@TK2MSFTN GP12.phx.gbl...[color=darkred]
            > > > Alvin,
            > > >
            > > >
            > > > It is wrong. For a form object, even if access to the object was
            > > > synchronized (which it is not), the windows handle and all operations[/color][/color][/color]
            on[color=blue][color=green]
            > > it[color=darkred]
            > > > (which the form encapsulates) requires that you make the call on the[/color]
            > > thread[color=darkred]
            > > > where the element was created.
            > > >
            > > > If you never run into problems in your scenario, then that's[/color][/color][/color]
            great,[color=blue][color=green]
            > > but[color=darkred]
            > > > if you do, it's something that you could have avoided now by using a[/color][/color]
            > call[color=green]
            > > to[color=darkred]
            > > > Invoke.
            > > >
            > > > Hope this helps.
            > > >
            > > >
            > > > --
            > > > - Nicholas Paldino [.NET/C# MVP]
            > > > - mvp@spam.guard. caspershouse.co m
            > > >
            > > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote[/color][/color]
            > in[color=green][color=darkred]
            > > > message news:%23zjChZko DHA.2364@TK2MSF TNGP11.phx.gbl. ..
            > > > > I dispensed some advice and its bugging me that it may not be 100%
            > > > accurate.
            > > > >
            > > > > Worker threads should not touch main thread objects. Everybody knows[/color]
            > > that[color=darkred]
            > > > > but if you pass a reference to a form object to a worker thread and[/color][/color]
            > you[color=green][color=darkred]
            > > > > touch that form on the worker thread. All is well.
            > > > >
            > > > > Is that right? Will all really be well? It's a reference to the form
            > > > object.
            > > > > The worker thread is still modifying the main threads object,[/color][/color]
            > reference[color=green]
            > > or[color=darkred]
            > > > > not, without using control invoke - that is, it isn't getting[/color][/color][/color]
            modified[color=blue][color=green][color=darkred]
            > > > from
            > > > > the thread owning the object in the correct way.
            > > > >
            > > > > I'm not sure I now agree that this is safe. Infact I now realize[/color][/color][/color]
            that[color=blue][color=green]
            > > this[color=darkred]
            > > > > is absolutely wrong.
            > > > > Any help here?
            > > > >
            > > > > --
            > > > >
            > > > >
            > > > > -----------
            > > > > Got TidBits?
            > > > > Get it here: www.networkip.net/tidbits
            > > > >
            > > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...