Multithreading - Accessing Object

G

Guest

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

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 said:
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]
- (e-mail address removed)

Tim said:
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.
 
T

Truong Hong Thi

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
 
G

Guest

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#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top