473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1266
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
6
by: Ilia | last post by:
Hi folks, I have some problems with ASP.NET Session State. The following simple program runs well if the Session State set as "InProc". If I switch to "SQLServer", the changes, made by the...
0
by: denis.cornehl | last post by:
Hi, I have an unusual Problem with DB2. It is DB2 Version 7 and Fixpack 13 under Windows. We have written an application server which is accessing db2 via c++ and the cli interface. We used...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.