Multithreading - Accessing Object

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

    #1

    Multithreading - Accessing Object


    An object 'X' is created inside a thread by object 'A'. The functionality of
    object 'X' is used only inside that thread. Inside the thread procedure, the
    object 'X's methods are called. That object has a Dispose method that needs
    to be called.

    The Dispose method of object 'X' has to be called finally when the object
    'A' is disposed. Is it okay to call the Dispose method of the object 'X'
    outside the thread procedure i.e. in the object A's Dispose method which is
    in another thread. The thread procedure has exited before the object 'A's
    Dispose method is called.




  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Multithreading - Accessing Object

    Tim,

    Dispose methods should be designed to be thread-safe as well, so whether
    or not it is safe to call depends on whether or not you made it thread safe.

    However, you state that object X is only used inside a thread. If that
    is the case, then why not just use a using statement so that when you exit
    the thread, you dispose of X, since you won't need it anymore?

    Hope this helps.


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

    "Tim" <Tim@discussion s.microsoft.com > wrote in message
    news:52A34643-7713-426E-8599-F9A610836E01@mi crosoft.com...[color=blue]
    >
    > An object 'X' is created inside a thread by object 'A'. The functionality
    > of
    > object 'X' is used only inside that thread. Inside the thread procedure,
    > the
    > object 'X's methods are called. That object has a Dispose method that
    > needs
    > to be called.
    >
    > The Dispose method of object 'X' has to be called finally when the object
    > 'A' is disposed. Is it okay to call the Dispose method of the object 'X'
    > outside the thread procedure i.e. in the object A's Dispose method which
    > is
    > in another thread. The thread procedure has exited before the object 'A's
    > Dispose method is called.
    >
    >
    >
    >[/color]


    Comment

    • Tim

      #3
      Re: Multithreading - Accessing Object

      Hi Nicholas,

      Thanks for the immediate response. If the Dispose method of the object 'X'
      is called in the thread, fxCop gives a 'Microsoft.Usag e' warning that the
      Dispose method of object 'X' has to be called in the Dispose method of object
      'A'. Any implications of ignoring this warning? What needs to be done to make
      the Dispose method 'Thread-safe'?

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      [color=blue]
      > Tim,
      >
      > Dispose methods should be designed to be thread-safe as well, so whether
      > or not it is safe to call depends on whether or not you made it thread safe.
      >
      > However, you state that object X is only used inside a thread. If that
      > is the case, then why not just use a using statement so that when you exit
      > the thread, you dispose of X, since you won't need it anymore?
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Tim" <Tim@discussion s.microsoft.com > wrote in message
      > news:52A34643-7713-426E-8599-F9A610836E01@mi crosoft.com...[color=green]
      > >
      > > An object 'X' is created inside a thread by object 'A'. The functionality
      > > of
      > > object 'X' is used only inside that thread. Inside the thread procedure,
      > > the
      > > object 'X's methods are called. That object has a Dispose method that
      > > needs
      > > to be called.
      > >
      > > The Dispose method of object 'X' has to be called finally when the object
      > > 'A' is disposed. Is it okay to call the Dispose method of the object 'X'
      > > outside the thread procedure i.e. in the object A's Dispose method which
      > > is
      > > in another thread. The thread procedure has exited before the object 'A's
      > > Dispose method is called.
      > >
      > >
      > >
      > >[/color]
      >
      >
      >[/color]

      Comment

      • Truong Hong Thi

        #4
        Re: Multithreading - Accessing Object

        Hi Tim,

        Is it necessary to declare X as class level variable? If you only use
        it in the thread proc, consider declare it as local variable, and use
        "using" or "finally" to dispose it.

        If it is necessary to declare it at class level (e.g. to maintain some
        state), then, it is likely that fxCop is right, you should dispose X in
        A's dispose. Because, as you said in your original post, "the thread
        procedure has exited before the object 'A's
        Dispose method is called.", so there is no need to worry about
        multi-threaded issues, which only matter if more than one thread access
        a resource *at the same time*.

        Regards,
        Thi

        Comment

        • Tim

          #5
          Re: Multithreading - Accessing Object

          Thanks Truong. I didn't realize that the fxcop error was because the object
          was declared at the class level. I have now declared the variable as a local
          variable. Also, Thanks Nicholas for the help regarding 'Using' in C#.

          "Truong Hong Thi" wrote:
          [color=blue]
          > Hi Tim,
          >
          > Is it necessary to declare X as class level variable? If you only use
          > it in the thread proc, consider declare it as local variable, and use
          > "using" or "finally" to dispose it.
          >
          > If it is necessary to declare it at class level (e.g. to maintain some
          > state), then, it is likely that fxCop is right, you should dispose X in
          > A's dispose. Because, as you said in your original post, "the thread
          > procedure has exited before the object 'A's
          > Dispose method is called.", so there is no need to worry about
          > multi-threaded issues, which only matter if more than one thread access
          > a resource *at the same time*.
          >
          > Regards,
          > Thi
          >
          >[/color]

          Comment

          Working...