471,354 Members | 1,284 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Multithreading - Accessing Object

Tim

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.


Dec 27 '05 #1
4 1191
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]
- mv*@spam.guard.caspershouse.com

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...

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.

Dec 27 '05 #2
Tim
Hi Nicholas,

Thanks for the immediate response. If the Dispose method of the object 'X'
is called in the thread, fxCop gives a 'Microsoft.Usage' 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:
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]
- mv*@spam.guard.caspershouse.com

"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...

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.


Dec 28 '05 #3
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

Dec 28 '05 #4
Tim
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:
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

Dec 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

47 posts views Thread by mihai | last post: by
16 posts views Thread by Robert Zurer | last post: by
reply views Thread by denis.cornehl | last post: by
2 posts views Thread by Pradnya Patil | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.